A hopefully simple question

Discussion in 'Perl Misc' started by Harold Pritchett, Feb 16, 2005.

  1. I am modifying a perl script which I did not write.

    I am at best a beginning perl programmer, but have been
    writing code in other languages for over 30 years.

    My question is the difference in the following two
    statements:

    print FILE "$_";
    print FILE ">$_";

    The file is the same, and is opened with the statement:

    &lopen($FH, ">>", "$opt_f$suffix") ||
    die("Can't append to $opt_f$suffix: $!");
    $is_open = 1;

    This is done in a subroutine and the file handle FILE is
    passed into the subroutine as FH. The file name obviously
    is passed in two pieces, $opt_f and $suffix.

    I've tried to find this on the web, but have failed
    miserably.

    Thanks

    Harold
     
    Harold Pritchett, Feb 16, 2005
    #1
    1. Advertising

  2. Harold Pritchett

    phaylon Guest

    Harold Pritchett wrote:

    > Subject: A hopefully simple question


    Weird problem ..

    > I've tried to find this on the web, but have failed miserably.


    ... but you're lucky, I've got a simple question, what came out when you
    tried? :D Also: Is that your real code? I was unable to find something in
    perldoc about lopen. Is this a subroutine as & indicates?

    --
    http://www.dunkelheit.at/

    Ordinary morality is only for ordinary people.
    -- Aleister Crowley
     
    phaylon, Feb 16, 2005
    #2
    1. Advertising

  3. Harold Pritchett

    thundergnat Guest

    Harold Pritchett wrote:

    >
    > My question is the difference in the following two
    > statements:
    >
    > print FILE "$_";
    > print FILE ">$_";
    >


    What happened when you tested it?



    my @words = qw/My question is the difference in the following two
    statements/;
    print "$_\n" for @words;
    print ">$_\n" for @words;
     
    thundergnat, Feb 16, 2005
    #3
  4. phaylon wrote:
    > Harold Pritchett wrote:
    >
    >
    >>Subject: A hopefully simple question

    >
    >
    > Weird problem ..
    >
    >
    >>I've tried to find this on the web, but have failed miserably.

    >
    >
    > .. but you're lucky, I've got a simple question, what came out when you
    > tried? :D Also: Is that your real code? I was unable to find something in
    > perldoc about lopen. Is this a subroutine as & indicates?
    >


    This is a subroutine, defined in shlock.pl and majordomo.pl.

    The code snippet from shlock.pl looks like this:

    # open a file locked for exclusive access;
    # we remember the name of the lock
    # file, so that we can delete it when we close the file
    #
    sub main'lopen {
    local($FH) = shift;
    local($mode) = shift;
    local($file) = shift;
    # $fm is what will actually get passed to open()
    local($fm) = "$mode$file";
    local($status);

    # create name for lock file
    local($lockfile) = $file;
    $lockfile =~ s,([^/]*)$,L.$1,;

    # force unqualified filehandles into callers' package
    local($package) = caller;
    $FH =~ s/^[^':]+$/$package'$&/;

    return undef unless &main'set_lock("$lockfile");

    # Got the lock; now try to open the file
    if ($status = open($FH, $fm)) {
    # File successfully opened; remember the lock file for deletion
    $lock_files[fileno($FH)] = "$lockfile";
    } else {
    # File wasn't successfully opened; delete the lock
    &main'free_lock($lockfile);
    }
    # return the success or failure of the open
    return $status;
    }

    Obviously, this code is a majordomo archive module, and I want to
    understand how it works so I can modify it's behavior.

    Harold
     
    Harold Pritchett, Feb 16, 2005
    #4
  5. Jim Gibson wrote:
    > In article <>, Harold Pritchett
    > <> wrote:
    >
    >
    >>I am modifying a perl script which I did not write.
    >>
    >>I am at best a beginning perl programmer, but have been
    >>writing code in other languages for over 30 years.
    >>
    >>My question is the difference in the following two
    >>statements:
    >>
    >> print FILE "$_";
    >> print FILE ">$_";

    >
    >
    > The second statement will print a '>' character before it prints the
    > contents of the variable $_ (to FILE). The first statement will not.


    Obvious now that I see it. Sometimes it's hard to tell data from code

    Thank you very much.

    Harold
     
    Harold Pritchett, Feb 16, 2005
    #5
  6. Harold Pritchett wrote:

    > phaylon wrote:
    >
    >> Harold Pritchett wrote:
    >>
    >>
    >>> Subject: A hopefully simple question

    >>
    >>
    >>
    >> Weird problem ..
    >>
    >>
    >>> I've tried to find this on the web, but have failed miserably.

    >>
    >>
    >>
    >> .. but you're lucky, I've got a simple question, what came out when you
    >> tried? :D Also: Is that your real code? I was unable to find something in
    >> perldoc about lopen. Is this a subroutine as & indicates?
    >>

    >
    > This is a subroutine, defined in shlock.pl and majordomo.pl.
    >
    > The code snippet from shlock.pl looks like...


    .... shlock.pl is a Perl4 script.

    There in general much better ways to do things than there were in Perl4.

    Comparatively few poeple here can remeber Perl4 well if at all. I, for
    example, have only been using Perl for a little over a decade so I
    hardly touched Perl4.

    Perl5 is almost but not quite 100% compatible with Perl4.

    Once of the things that is different is what happens if you use a GLOB
    (kinda poor-man's reference) in a string context. In Perl4 the
    delimiter between the namespace and the symbol is a single quote, in
    Perl5 it's a double colon.

    > # open a file locked for exclusive access;
    > # we remember the name of the lock
    > # file, so that we can delete it when we close the file
    > #
    > sub main'lopen {
    > local($FH) = shift;
    > local($mode) = shift;
    > local($file) = shift;
    > # $fm is what will actually get passed to open()
    > local($fm) = "$mode$file";
    > local($status);
    >
    > # create name for lock file
    > local($lockfile) = $file;
    > $lockfile =~ s,([^/]*)$,L.$1,;
    >
    > # force unqualified filehandles into callers' package
    > local($package) = caller;
    > $FH =~ s/^[^':]+$/$package'$&/;
    >
    > return undef unless &main'set_lock("$lockfile");
    >
    > # Got the lock; now try to open the file
    > if ($status = open($FH, $fm)) {
    > # File successfully opened; remember the lock file for deletion
    > $lock_files[fileno($FH)] = "$lockfile";
    > } else {
    > # File wasn't successfully opened; delete the lock
    > &main'free_lock($lockfile);
    > }
    > # return the success or failure of the open
    > return $status;
    > }
    >
    > Obviously, this code is a majordomo archive module, and I want to
    > understand how it works so I can modify it's behavior.


    So, can you point out the parts that you are finding difficulty finding
    in the refernce manuals or the passages in the reference manuals you are
    finding unclear.

    Note: to make sense of this code you using contemporary documentation
    you need to translate all the singe quetes in the middle of symbol
    names into double colons.
     
    Brian McCauley, Feb 16, 2005
    #6
  7. Harold Pritchett wrote:

    > I am modifying a perl script which I did not write.
    >
    > I am at best a beginning perl programmer, but have been
    > writing code in other languages for over 30 years.
    >
    > My question is the difference in the following two
    > statements:
    >
    > print FILE "$_";


    This prints the contents of $_ to the filehandle FILE.

    > print FILE ">$_";


    This prints ">" and then the contents of $_ to the
    filehandle FILE.
    >
    > The file is the same, and is opened with the statement:
    >
    > &lopen($FH, ">>", "$opt_f$suffix") ||
    > die("Can't append to $opt_f$suffix: $!");
    > $is_open = 1;
    >
    > This is done in a subroutine and the file handle FILE is
    > passed into the subroutine as FH. The file name obviously
    > is passed in two pieces, $opt_f and $suffix.
    >
    > I've tried to find this on the web, but have failed
    > miserably.
    >
    > Thanks
    >
    > Harold


    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Feb 16, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Gardner
    Replies:
    0
    Views:
    372
    Chris Gardner
    Oct 29, 2003
  2. Jon Thackray

    Hopefully simple XPath question

    Jon Thackray, Nov 10, 2004, in forum: XML
    Replies:
    9
    Views:
    439
    Joris Gillis
    Nov 13, 2004
  3. Replies:
    4
    Views:
    281
  4. Replies:
    1
    Views:
    259
  5. Chuft Captain

    A hopefully simple question

    Chuft Captain, Apr 9, 2009, in forum: Ruby
    Replies:
    5
    Views:
    183
    Chuft Captain
    Apr 9, 2009
Loading...

Share This Page