Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Perl bug ?

by choroba (Abbot)
on Feb 06, 2013 at 11:54 UTC ( #1017394=note: print w/ replies, xml ) Need Help??


in reply to Perl bug ?

You have stumbled upon autovivification. After expanding $error->[0] in the double quoted string, Perl creates the referenced array for you, so $error is now [].

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Perl bug ?
Select or Download Code
Re^2: Perl bug ?
by vsespb (Hermit) on Feb 06, 2013 at 11:58 UTC
    You are right.
    #!/usr/bin/perl my $errors = undef; $errors->[0]; print $errors
    prints ARRAY(0x15bad48). I didn't realize that autovivification happens even on read-only access =)
Re^2: Perl bug ?
by vsespb (Hermit) on Feb 06, 2013 at 12:06 UTC
    But why ARRAY ref passed to subroutine ? Argument is
    ($errors && ($errors->[0] =~ /Journal file not found/i))
    so it should be evaluated to false ?
      Update: Disregard. See rather Re^5: Perl bug ?.

      The order in which arguments to subroutines are processed is not guaranteed:

      sub test { say for @_ } test($x=0, $x=$x+1, $x++, ++$x);
      Returns:
      3 3 1 3
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        but anyway this
        ($errors && ($errors->[0] =~ /Journal file not found/i))
        should be true only and only if $errors->[0] contains Journal file not found ?
        no? I don't see your example as a proof for that.
        argument 1: $x=0 $x is set to 0 and $x (or an alias) is passed as argument 1
        argument 2: $x=$x+1 $x is set to 0+1 = 1 and $x (or an alias) is passed as argument 2
        argument 3: $x++ $x is incremented and the return value of the post increment (1) is passed as the argument 3
        argument 4: ++$x $x is incremented and the return value of the pre increment (3) is passed as argument 4.

        $x is now 3.
        argument 1 is an alias for $x. 3
        argument 2 is an alias for $x. 3
        argument 3 is 1.
        argument 4 is 3.
      the first call of ok():
      the first argument is $errors && ($errors->[0] =~ /.../i) which results in $errors being passed as the first argument. before it is passed, array element 0 gets autovivivied though by usage in the second argument.

      the second call:
      $errors is now true, so the matching is executed and its result would the empty string ("false"). but since the matching is executed in list context, the result is an empty list, so what you wrote as a second argument becomes now the first argument.
        Why I have
        [[[ARRAY(0x137cd48)]]] [[[some text 1 ]]]
        then ? First argument should be true only if $errors->[0] contains string /Journal file not found/ no ?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1017394]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-11-23 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (128 votes), past polls