Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

for ($@)

by szabgab (Priest)
on Jan 18, 2005 at 21:24 UTC ( #423189=perlquestion: print w/replies, xml ) Need Help??

szabgab has asked for the wisdom of the Perl Monks concerning the following question:

In a code I am maintainng it sais
eval { # bla bla }; for ($@) { print '$@'; }
Is this for($@) some expression I don't know or should I just replace if with if($@) ?

Replies are listed 'Best First'.
Re: for ($@)
by hardburn (Abbot) on Jan 18, 2005 at 21:30 UTC

    That particular use of for sets the $_ to whatever was in $@. The code as presented doesn't do anything useful with $_, so you can replace it with if($@). The for expression is normally a looping construct, but it isn't be used that way here.

    The single quotes around $@ are also probably incorrect. You'll need double quotes so you can print out the value of $@. Or just leave them off entirely and Perl will do the Right Thing in this case.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: for ($@)
by holli (Abbot) on Jan 18, 2005 at 21:35 UTC
    see perlvar:
    The Perl syntax error message from the last eval() operator. If $@ is the null string, the last eval() parsed and executed correctly (although the operations you invoked may have failed in the normal fashion). (Mnemonic: Where was the syntax error ``at''?) Warning messages are not collected in this variable. You can, however, set up a routine to process warnings by setting $SIG{__WARN__} as described below. Also see Error Indicators.
    In that sense, $@ is a scalar and therefore, looping about a scalar makes no sense and you can replace that code with:
    print $@ if $@;

    holli, regexed monk
Re: for ($@)
by edoc (Chaplain) on Jan 19, 2005 at 00:48 UTC


    This looks to me like a first revision which was intended to be developed further. As mentioned by hardburn the for ($@) sets $_ to the value of $@. Why would we want to do that? So we could then do something like:

    eval { # bla bla }; for ($@) { /NOT FOUND/ && do { print 'Where'd I put that?'; last; }; print 'Na,ah. Not for you!' if /FORBIDDEN/; }

    Each of those is testing the string value of $@



Re: for ($@)
by Beechbone (Friar) on Jan 19, 2005 at 14:54 UTC
    If you change the for() to if() you'll cange the semantics. As $@ is always one scalar value (just like all scalar variables) the for($@) will always be executed once, regardless of the value of $@. So
    for ($@) { print $@; }
    is the same as:
    print $@;
    But I'd say the original semantics was wrong, so you'd better use the if(). I'd even suggest you use if (my $e = $@) {... to get rid of some nasty side-effects like $@ getting cleared by some sub you call inside your if-block...

    Search, Ask, Know

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://423189]
Approved by holli
Front-paged by tye
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2022-05-17 03:40 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (65 votes). Check out past polls.