Jim Nathan wrote:
>
> I'm new to Perl and I just got a position at a company where we use a
> couple of Perl scripts to both set or reset passwords and add user
> accounts. These come in handy when users request accounts on multiple
> servers. A guy who no longer works there wrote at least one of the
> scripts and I am told that he was a really a hot programmer.

That is most likely to mean that it was hard to understand what he wrote. make
up your own mind :)

> In looking over the programs, I was curious about a couple of things. One
> was the use of this statement:
>
>   $| = 1;
>
> of the $| (dollar-pipe) variable?

Yes. Any identifier that doesn't begin with an alphabetic character or an
underscore is a special one. Take a look at

  perldoc perlvar

for a complete list.

Setting $| controls the autoflush of the currently-selected filehandle.
Autoflush determines whether the the data is output to the the device after each
call to print or just left until the buffer fills. It is most often seen enabled
on STDOUT so that the output a program generates can be seen while it is 
running.

It is much more manageable to use the IO:Handle module and write, for example

  STDOUT->autoflush;

to enable it or

  STDOUT->autoflush(0);

to disable it. It is much more readable that way and can be used to control file
handles that aren't the currently-selected one. IO::Handle is a standard module
and so needn't be installed.

> The other question concerned this construct:
> 
>   open(LFD, ">abc.log");
> 
> My question here is that I can't find an example of open being used in this
> syntax and was also wondering whether LFD had any special meaning.
> Admittedly, my primary resources so far are Learning Perl and Intermediate
> Perl, both by O'Reilly.

I don't have a copy of either book to hand, but I would be very surprised if
it's not in both of them somewhere. LFD is the filehandle, and it's certainly
documented in

  perldoc perlopentut

which says,

> For example:
>
>   open(INFO,      "datafile") || die("can't open datafile: $!");
>   open(INFO,   "<  datafile") || die("can't open datafile: $!");
>   open(RESULTS,">  runstats") || die("can't open runstats: $!");
>   open(LOG,    ">> logfile ") || die("can't open logfile:  $!");

and you should refer there if you have problems understanding the syntax.

Which raises a few other points. First of all, if your hot programmer has indeed
written

  open(LFD, ">abc.log");

without checking the status of the open then he has comitted a cardinal sin. If
the open fails for any reason then the rest of the program will continue to run
regardless without storing any data. Also, as '>abc.log' is a valid filename in
itself on some platforms, it is best practice to open files using the
three-parameter form of the call, so that

  open LFD, '>', 'abc.log' or die $!;

is the best way to have written it.

Better still would be to use lexical filehandles, as in

  open my $lfd, '>', 'abc.log' or die $!;

but that may need many changes elsewhere in the code, so for now I suggest you
remember it only for new software.

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to