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"
}
Related
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 1 year ago.
Improve this question
I wrote in code
my $sql = ...
my $sth = $dbh->prepare($sql);
eval {
$sth->execute;
}
or do {
# die "SQL Error: $DBI::errstr\n";
addToLog("SQL Error: $DBI::errstr\n");
sleep(30);
next mysql_recover;
};
but checker swears I can't use next inside eval. How to rewrite otherwise?
The problem is in a scope enclosing the shown code (which isn't shown). From next
next LABEL
next EXPR
next
The next command is like the continue statement in C; it starts the next iteration of the loop [...]
So the shown code should be in a loop, with the label mysql_recover. Or in a block with such a label, since a block is a loop that executes once (but then next should be last).
However, we can't tell what is wrong without more code or the actual error message. The shown code doesn't give any hints since a next LABEL; is legit only to target structures that are subject to flow control so the mere acceptance of that statement implies that next is fine.
That do block is not a part of the eval so I don't know what your checker might mean.
But, if there were a next inside of eval, then
eval BLOCK does not count as a loop, so the loop control statements next, last, or redo cannot be used to leave or restart the block.
I'd like to also comment on that eval.
The $sth->execute shown as the sole statement in the eval can return an undef, if there is an error -- and so your eval-handler (the do block) would trigger in that case. If that's your intent then fine, but that'd be confusing since an eval is meant to guard against exceptions (a die).
A full idiom is eval { ...; 1 } or do {...};, so the eval always returns success (that 1) unless a die was thrown . So or do runs only if there was an exception, not on "ordinary" errors (like an undef from DBI...).
Another way to handle that would be to explicitly check for errors from $sth->execute, what need be done anyway. (But then add that 1 as well, against an unexpected false. Why not.)
(There is still some fine print about $# but that would take us elsewhere)
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 1 year ago.
Improve this question
I have a variable $abc that contains the line like below:
abc.txt -> check a/b/c test
I need to get abc.txt in another variable say $bcd and a/b/c in the variable say $xyz. I have the regex to do so but I don't know how I can do it using perl as in my knowledge perl grep and perl map can be used on arrays only not variables.
my ($bcd) = split(/\s*->\s*/, $abc, 2);
my $bcd = $abc =~ s/\s*->.*//sr;
my ($bcd) = $abc =~ /^(?:(?!\s*->).)*)/s;
my ($bcd) = $abc =~ /^(.*?)\s*->/s;
All but the last returns the entire input string if there's no ->.
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 3 years ago.
Improve this question
As i have read ,"There’s another problem with this subroutine.This subroutine works properly only if called with exactly two
Excess parameters are ignored—since the subroutine never looks at $_[2] "
but when I passed multiple arguments its working,so I'm not able to conclude the above statement so can anyone help me in sorting out this problem.
sub privacy{
$_[0]+$_[1]+$_[2]+$_[3];
}
$x=&privacy(3,4,5,6);
print $x,"\n";`
Expected:
7
Actual:
18
but this result is contradicting.
I think you're reading Learning Perl. We present a subroutine that finds the maximum of two arguments. It looks something like this:
sub max {
if( $_[0] > $_[1] ) { ... }
else { $_[0] }
}
If you call it with two arguments, it works out:
max( 1, 2 )
max( 5, 6 )
max( 9, 0 )
If you call it with three arguments, that particular subroutine ignores the third argument because that particular subroutine only looks at the first two:
max( 1, 2, 3 ) # ignores 3, so returns 2
You can write some other subroutine that looks at more arguments, but that's a different subroutine.
Unless you tell Perl otherwise (and I'll ignore that here), it will take as many arguments as you care to give it and they all show up in the special variable #_. How you use that is up to you.
You are misunderstanding the meaning of the sentence:
Excess parameters are ignored—since the subroutine never looks at
$_[2]
What is meant by that is that in a perl subroutine (as in any language) if you don't use a parameter then it's not used.
In the particular example that the above sentence refers to, parameter 2, etc., are not used in the code so they are 'ignored'. They're available if you want to use them, they're just not used in that particular example.
However in YOUR code you are using them.
If that's what they said, and if that's the code they were talking about, then they are wrong. The sub clearly "looks at $_[2]".
3+4+5+6 is 18, and that's exactly what the code the outputs.
$ perl -e'
sub privacy{
$_[0]+$_[1]+$_[2]+$_[3];
}
$x=&privacy(3,4,5,6);
print $x,"\n";
'
18
Note that the code has issues (& used on the sub call even though they are no prototypes to override, and $x isn't scoped), so you shouldn't be taking lessons from whomever wrote that code.
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does 1; mean in Perl?
I'm new to Perl and learning how to build a class with Perl.
As from this example:
http://www.tutorialspoint.com/perl/perl_oo_perl.htm, I see a line which is written as "1;"
just can't find information about this interesting line from perldoc.perl.org
Do you know what it is? And why is it there in the Perl source code?
A module is normally a bunch of subroutine definitions, but it can also include code that is not in a subroutine (such as initialisation code). This code might fail, so Perl lets you indicate this by returning false whereupon Perl aborts with an error.
However, since the default return value is false, we must explicitly return true at the end of a module.
The perldocs have this to say:
The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1; , in case you add more statements