Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to do command line argument parsing for a directory path in perl. I want to make this argument as optional.
so when the user gives path, it showed be assigned to a variable $release_model else it will execute other code I have written for finding directory from main directory. I am new to perl but somehow coded following. Can anybody help?
Getopt::Long
my $command_line = $GetOptions(\%Opts, "help|h","email=s#","test","model");
if($command_line==0){
print "$PROGRAM: no arguments are given";
Usage{};
}
else die "No arguments were given"
But it doesn't accept model as optional argument and throws error.
I just started working with perl.
It is quite hard to guess what exactly you are after as the code provided contains lots of errors and other features not described. But to start learning with something simple, here is something that I hope matches your requirements.
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my $release_model = '/path/to/"main directory"'; # default value
GetOptions( 'model=s' => \$release_model )
or die("Error in command line arguments\n");
print "Release model is: $release_model\n";
If you save this to a file (e.g. my_program.pl) and make it executable then you can see it provides these features:
If you call it without arguments ./my_program.pl, the default value of $release_model will be used.
If you call it with argument model (e.g. ./my_program.pl --model /another/directory), the provided value will be assigned to $release_model.
If you call it with wrong arguments (e.g. ./my_program.pl --mdoel), it prints reasonable error message and exits.
Try it yourself. And go and read some tutorial on Perl if you want to do some serious work.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am looking at Perl script and I have a line:
my (#parmTypesList) = #$function_type_ref;
$function_type_ref is a string that is passed to the current function.
What does #$ means ?????
#$var is short for #{ $var } and equivalent to $var->#*. It is an array dereference.
It expects $var to hold a reference to an array, and it produces one of the following:
In scalar context, the number of elements of the array
In list context, the value of the elements of the array.
When an array is expected (e.g. #$var = ..., push #$var, ...), the array itself.
In this case, it's the second.
Despite the name, in no way would a reference to a function work.
But a string might work, since a string can be used as a reference. In this situation, the string is expected to be the name of the variable. This is a horrible, horrible thing to do, so we tell Perl not to let us do this by using use strict; or use v5.12;.
Just to be clear: You should ALWAYS use use strict; or equivalent.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have set the WORKAREA to some path and in Perl code I have defined like this:
eval 'exec $ENV{'WORKAREA'}/some/path'
if 0;
I am getting error that path $ENV{'WORKAREA'}/some/path is not defined. Anyone know to define this?
I'm not sure what you are copying there, but there are a few problems.
First, look inside the string that you give to eval. It's not valid Perl:
exec $ENV{'WORKAREA'}/some/path
You're trying to construct the string that represents the path to the program you want to exec, so quote the whole thing (like any other string):
exec "$ENV{'WORKAREA'}/some/path"
or even better, use generalized quoting so you won't have to escape something later:
exec q($ENV{'WORKAREA'}/some/path)
Note that I used q() here. You don't want that string to interpolate because that variable will have already been interpolated by the string you give to eval:
eval "exec q($ENV{'WORKAREA'}/some/path)"
I'm not sure why you eval this though. Maybe you think that the eval will shield you from some magic. But, exec isn't doing anything that magical. It's presence in your program shouldn't affect anything else:
if( 0 ) { # why are you doing this?
exec "$ENV{'WORKAREA'}/some/path"
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
&verify_se_linecount ( \#rows );
### Drop the modified file
open ( OUT, "> $RESOLVED_DIR\\$filenameonly" );
# Loop through each row in the EDI file
foreach my $row ( #rows ) {
print OUT $row . "$ROW_DELIM"; # Line 164
}
close OUT;
I have a Perl script (the code above is part of it) that works perfectly on a test server but shows a compilation error on a production server. The error is
Global symbol `$ROW_DELIM` requires explicit package name at <script_name> line 164
The variable is declared with our $ROW_DELIM in a package which is imported in this script. It does not show any error for other objects used from that package.
Without being able to see how you export/import the function - it'll be one of these things:
You've declared it with my in your package.
You aren't exporting it like you think
You aren't importing it like you think.
Try $OtherPackageName::ROW_DELIM and see if that works.
It isn't enough to declare the variable in a module that your main code uses: you have to export it from that module to make it visible elsewhere
If you need any further help then you must show the contents of the module, together with the statement that imports it into the main code
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i have assigned a global variable at start of my script which is empty string and i assigned a value to that inside subroutine . When the script enters the subroutine second time that variable is null and assigned new value .
I need to have the variable name constant for some subroutine calls and then change the value in the subroutine when my condition match
first time it call the subroutine this variable will be empty enter the loop and in the loop i will assign the variable.. next time when it enter the sub routine i want to use that variable value until the condition is met .
Here is the sample code
#!/usr/bin/perl
my $Next_5minus = '';
sub write_alog {
if (my $Next_5minus eq '')
{
........
.........
}
elsif ( $start_mtime < $end_mtime )
{
say $fh join("\n", #$alog);
}
elsif ( $start_mtime > $end_mtime )
{
my $Next_5minus = <will assign value>
..........
}
}
If you want people to help you with your problems, it's polite to make it as simple as possible for them to help you. As a minimum, you should do the following:
Provide a short, self-contained, runnable program that demonstrates your problem.
Clean up the indentation in your code to make it easy to follow.
Add use strict and use warnings to your code and clean up the problems they point out.
In this case, I suspect you'll see warnings about variables that mask variables of the same name. You define three copies of your $Next_5minus variable. Each of them will be initialised as undef as it is created and will disappear as it goes out of scope.
Try removing the extraneous my statements from your code and see if that fixes your problem.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am using CGI in strict mode and a bit confused with variables. I am reading a file that has two lines. Storing both in two variables. But when i try outputing them using html, it says global variable error
This is what I am doing
open TEXT, "filename";
$title = <TEXT>;
$about = <TEXT>;
close TEXT;
but this gives the global variable error. whats the best way to fix this?
You need to declare variable with my to make its scope local. This is the best practice and compulsory when using strict
use strict;
use warnings;
open my $fh, '<', 'filename' or die $!;
my ( $title, $about ) = <$fh>;
close $fh;
Further improvements:
Avoided bareword file handles (e.g. FILE). Instead use local file handles such as my $fh
Used error handling with die when dealing with file processing
Combined assignment of $title and $about as suggested by #Suic
use warnings to display what's going wrong as pointed out by #TLP