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

I am trying to debug anothers (author unknown) perl code for a friend and have a question.. I am not familiar with the following code
 if ( !open ($AB::XFORM_FILE, $ARGV[0]))
I know it's saying if not open...
does that also open the file if it is not opened?
I ask because I cannot find another open statement
If someone could interpret this for me I would buy the beers (root beer of course)!!
Also, I have never used
::
in an open statement so I don't know exactly what this means.

Replies are listed 'Best First'.
Re: Open statement
by imp (Priest) on Jul 21, 2006 at 11:59 UTC
    When opening a file you can either specify the filehandle, or you can provide an undefined scalar (or existing reference) - which will then hold an anonymous reference to a newly created filehandle.

    Typically one opens file lie this:

    open SOME_FH, ">", "somefile" or die "Failed to open somefile. $!"; print SOME_FH "Test\n";
    But it's also ok to do this
    my $fh = undef; open $fh, ">", "somefile" or die "Failed to open somefile. $!"; print $fh "Test\n";
    In the code you listed the programmer chose the second option, but instead of declaring a lexical variable to keep the filehandle in, he uses the variable named $XFORM_FILE in the AB package.

    You can read more about packages here

      Thanks loads.. I did read the open docs but missed the meaning of not opened (i guess I scanned the doc rather than read)..
Re: Open statement
by shmem (Chancellor) on Jul 21, 2006 at 12:24 UTC
    Also, I have never used
    ::
    in an open statement so I don't know exactly what this means.
    The :: is a package name qualifier. So, $AB::XFORM_FILE is the variable $XFORM_FILE in the package AB (also referred to as namespace, symbol table.) See perlmod, section Symbol Tables.

    Somewhere in the file you are looking at there's might be either the statement use AB or package AB. See use and package.

    --shmem

    update - inserted "might", see follow-up. As imp correctly notes, a symbol table can but must not be created via a package or use statement.

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      There might not be a use AB or package AB, the original programmer might just be using AB as a global variable namespace.
Re: Open statement
by davorg (Chancellor) on Jul 21, 2006 at 11:36 UTC

    When faced with a problem like this, a good start would be to read the documentation for open. That says this about the return value from open:

    Open returns nonzero upon success, the undefined value otherwise

    So your code is called open, checking the return value and taking appropriate action if it fails.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Open statement
by Moron (Curate) on Jul 21, 2006 at 13:18 UTC
    Extra hint: It is actually saying, 'if the call to "open" fails' rather than 'if not open'.

    -M

    Free your mind