[15689] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3102 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri May 19 18:15:34 2000

Date: Fri, 19 May 2000 15:15:23 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <958774523-v9-i3102@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 19 May 2000     Volume: 9 Number: 3102

Today's topics:
        the use of $_ <mcdonabNO@SPAMyahoo.com>
    Re: the use of $_ <godzilla@stomp.stomp.tokyo>
    Re: the use of $_ <jboesNOjbSPAM@qtm.net.invalid>
    Re: the use of $_ (Gordon Clemmons)
    Re: the use of $_ <bmb@dataserv.libs.uga.edu>
    Re: the use of $_ (Tad McClellan)
    Re: use and require error (Abigail)
        Using a file handle in 2 contexts simultaneously... <pgoetz@math.utexas.edu>
    Re: Using a file handle in 2 contexts simultaneously... <jhelman@wsb.com>
    Re: variable.pm (Abigail)
    Re: zen and the art of trolling [OT] (Gordon Clemmons)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 19 May 2000 11:53:29 -0700
From: "Brian McDonald" <mcdonabNO@SPAMyahoo.com>
Subject: the use of $_
Message-Id: <8g42l3$k6c$1@slb7.atl.mindspring.net>

Hi.

I am confused by the manner in which Perl assigns values to the default
variables.

For example, I have written a script that converts a particularly formatted
text file to an XML document. Now I am modifying it so that I can list the
text files as arguments on the command line. So, I am wrapping a new while
statement around the while that does the core text processing.

# get next input file from command line
while (<>) {
    my $outfile = $_;
    $outfile =~ s/.txt/.xml;

    # open file for read
    open (TXTIN,$_) || die "";
    # open file for output
    open (XMLOUT,">$outfile") || die "";

    # print xml file header declaration, etc.
    ...

    # THE ORGINAL OUTER LOOP
    # loop over all lines in the file
    while (<TXTIN>) {
        chomp;
        # check if this is an "identifier" or a line of pure text
        if (/FIRSTNAME=*/) {
            # get first name
            s/FIRSTNAME=//;
            $firstnm = $_;
        } elsif (/LASTNAME=*/) {
            # again
        } elsif (/TITLE=*/) {
            ...
        }
    ...
    }
}

The problem here is (clearly) that I am already using $_ to hold the input
file name. What are my options?

I suspect that this is a stupid question, but the camel book doesn't provide
a discourse on the use of $_, and the documentation that comes along with
ActivePerl doesn't provide me with either the detail or the clarity I need.
Unless there's another source of info than "perlvar".

Any help is appreciated!

Brian








------------------------------

Date: Fri, 19 May 2000 12:37:24 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: the use of $_
Message-Id: <392597F4.F96C2BE9@stomp.stomp.tokyo>

Brian McDonald wrote:

> The problem here is (clearly) that I am already 
> using $_ to hold the input file name. What are 
> my options?

> I suspect that this is a stupid question, but the 
> camel book doesn't provide a discourse on the use 
> of $_, and the documentation that comes along with
> ActivePerl doesn't provide me with either the detail 
> or the clarity I need.

> Any help is appreciated!

It's local bigotry. Oh my $word.

Read these two sites well. I believe this will
answer some, perhaps most of your questions.


http://www.perl.com/CPAN-local/doc/manual/html/pod/perlsub.html#Temporary_Values_via_local_

http://www.plover.com/~mjd/perl/local.html



First link will probably word wrap in
your news reader. Copy and paste it into
your location bar as a single line URL
with no spaces included.

Second link is constantly down. You may
have to try for days to connect. Takes me
an average of five days to connect to
Plover's site.

Godzilla!


------------------------------

Date: Fri, 19 May 2000 13:21:45 -0700
From: Mur <jboesNOjbSPAM@qtm.net.invalid>
Subject: Re: the use of $_
Message-Id: <1c4becc0.90be9c4c@usw-ex0101-006.remarq.com>

In article <392597F4.F96C2BE9@stomp.stomp.tokyo>, "Godzilla!"
<godzilla@stomp.stomp.tokyo> wrote:

>http://www.plover.com/~mjd/perl/local.html
>
>Second link is constantly down. You may
>have to try for days to connect. Takes me
>an average of five days to connect to
>Plover's site.

Huh? Gotta be something at your end (or your ISP's), because I
have no problems here. Do you get DNS errors? If so, then report
this to your ISP.



Jeff Boes//ICQ=3394914//Yahoo!=jeffboes//AOL IM=jboes
  //home=jboes@qtm.net//professional=mur@consultant.com

* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!



------------------------------

Date: 19 May 2000 20:39:11 GMT
From: perl_phreak@yahoo.com (Gordon Clemmons)
Subject: Re: the use of $_
Message-Id: <8F3988BD4nospamblahcom@206.165.3.70>

godzilla@stomp.stomp.tokyo (Godzilla!) wrote in

>It's local bigotry. Oh my $word.
>
>Read these two sites well. I believe this will
>answer some, perhaps most of your questions.
>
>
>http://www.perl.com/CPAN-local/doc/manual/html/pod/perlsub.html#Temporary
>_Values_via_local_ 

Did you notice the introductory NOTE: at the top of your 
link that you use to defend your use of local?

-- Gordon


------------------------------

Date: Fri, 19 May 2000 16:45:16 -0400
From: Brad Baxter <bmb@dataserv.libs.uga.edu>
Subject: Re: the use of $_
Message-Id: <Pine.GSO.4.21.0005191639240.17334-100000@dataserv.libs.uga.edu>

On Fri, 19 May 2000, Brian McDonald wrote:

> Hi.
> 
> I am confused by the manner in which Perl assigns values to the default
> variables.
> 
> For example, I have written a script that converts a particularly formatted
> text file to an XML document. Now I am modifying it so that I can list the
> text files as arguments on the command line. So, I am wrapping a new while
> statement around the while that does the core text processing.
> 
> # get next input file from command line
> while (<>) {
>     my $outfile = $_;
>     $outfile =~ s/.txt/.xml;
> 
>     # open file for read
>     open (TXTIN,$_) || die "";
>     # open file for output
>     open (XMLOUT,">$outfile") || die "";
> 
>     # print xml file header declaration, etc.
>     ...
> 
>     # THE ORGINAL OUTER LOOP
>     # loop over all lines in the file
>     while (<TXTIN>) {
>         chomp;
>         # check if this is an "identifier" or a line of pure text
>         if (/FIRSTNAME=*/) {
>             # get first name
>             s/FIRSTNAME=//;
>             $firstnm = $_;
>         } elsif (/LASTNAME=*/) {
>             # again
>         } elsif (/TITLE=*/) {
>             ...
>         }
>     ...
>     }
> }
> 
> The problem here is (clearly) that I am already using $_ to hold the input
> file name. What are my options?

I'm not sure I see a problem.  Does the code work?  I can't see any
collisions.  You could avoid the issue with

   while( defined( $linein = <> ) ) {

and then use $linein explicitly where you now use $_ explicitly or
implicitly.

__ 
Brad



------------------------------

Date: Fri, 19 May 2000 15:54:39 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: the use of $_
Message-Id: <slrn8ib6vv.920.tadmc@magna.metronet.com>

On Fri, 19 May 2000 11:53:29 -0700, Brian McDonald <mcdonabNO@SPAMyahoo.com> wrote:

>I am confused by the manner in which Perl assigns values to the default
>variables.


And also, it appears, by how command line args are passed  :-)

You are not confused by "while(<>)" since you say below that
you see what the problem is.


>Now I am modifying it so that I can list the
>text files as arguments on the command line. 


The command line args are in the @ARGV array.

If you want to process them yourself (instead of having <>
process them for you), then process them yourself  :-)


> So, I am wrapping a new while
>statement around the while that does the core text processing.
>
># get next input file from command line
>while (<>) {


That does NOT "get next input file from command line"!

It:

   opens the file named on the command line

   reads lines from that file

You are processing the _contents_ of the file, not the _filename_ here.


>    my $outfile = $_;
>    $outfile =~ s/.txt/.xml;


   foreach my $infile ( @ARGV ) {  # foreach instead of while
      my $outfile = $infile;
      $outfile =~ s/.txt/.xml;
      ...


Or, if you are really attached to while():

   while ( my $infile = shift ) { # shift defaults to shifting @ARGV in main
      my $outfile = $infile;
      $outfile =~ s/.txt/.xml;
      ...



>    # open file for read
>    open (TXTIN,$_) || die "";


That is a profoundly sad diagnostic.   :-(


   open (TXTIN, $infile) || die "could not open '$infile'  $!";


>    # open file for output
>    open (XMLOUT,">$outfile") || die "";
>
>    while (<TXTIN>) {


You can continue using $_ for that loop if you like.


>The problem here is (clearly) that I am already using $_ to hold the input
>file name. What are my options?


Use a variable with different name for one or the other of them.


>I suspect that this is a stupid question, but the camel book doesn't provide
>a discourse on the use of $_, and the documentation that comes along with
>ActivePerl doesn't provide me with either the detail or the clarity I need.
>Unless there's another source of info than "perlvar".


You never _have_ to use $_.

You can _always_ fall back to naming all of your own variables,
it just means more typing.


-- 
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


------------------------------

Date: 19 May 2000 18:51:21 GMT
From: abigail@foad.org (Abigail)
Subject: Re: use and require error
Message-Id: <slrn8ib399.prq.abigail@ucan.foad.org>

On Thu, 18 May 2000 11:38:02 +0200, Matteo Palmieri <i094034@hotmail.com> wrote:
++ I have installed perl 5.004_03 on OS390. It was a binary version (I have
++ not compiled at all). The problem is that neither the "use" directive
++ nor the "require" are understood by perl.
++ 
++ This simple script ...
++ #!/usr/bin/perl
++ use B;
++ 
++ ..gets an error

Well, what kind of error?



Abigail


------------------------------

Date: Fri, 19 May 2000 16:28:40 -0500
From: Patrick Goetz <pgoetz@math.utexas.edu>
Subject: Using a file handle in 2 contexts simultaneously...
Message-Id: <3925B208.64E1205@math.utexas.edu>

This isn't really a problem, since I know exactly how to get around it,
but it is bothering me, and I'm thinking there must be a canonical
solution to something this basic.

Suppose I want to have a script which takes a single input file and
writes output to either a file or STDOUT based on the command line
parameters given; i.e. if the user types

     prog infile xxx

prog reads data from infile and writes to xxx, and if the user types

     prog infile

the output is written to standard out, with anything else generating an
die/error message.  Ignoring all extraneous features such as error
checking for the moment, the obvious way to do this is something like
this:

   if ( scalar(@ARGV) == 1 ) {
     open (INPUT, "$ARGV[0]");
     *OUTPUT = *STDOUT;
   }
   if ( scalar(@ARGV) == 2 ) {
     open (INPUT, "$ARGV[0]");
     open(OUTPUT, "$ARGV[1]");
   }
<do some stuff>
   print OUTPUT $stuff;

My problem is that I don't want to use a typeglob assignment to set
OUTPUT to be the same as STDOUT for aesthetic reasons (and because
I shouldn't have to - at least until I stand corrected).  However, it
doesn't seem to be possible, because, as far as I can tell, I can't set
OUTPUT to be a reference to STDOUT:

   $OUTPUT = *STDOUT;

because you can't open a filehandle as a reference:

   open( $OUTPUT, "$ARGV[1]");

gives an error, so I can't just change all the OUTPUT's to $OUTPUT's in
the script.

So, is there some way to not do a typeglob assignment, or is there a
better, canonical way of solving this problem?





------------------------------

Date: Fri, 19 May 2000 22:01:58 GMT
From: Jeff Helman <jhelman@wsb.com>
Subject: Re: Using a file handle in 2 contexts simultaneously...
Message-Id: <3925B9F9.4E9747CE@wsb.com>

How 'bout:

## MAKE SURE FILE ONE EXISTS
my $OutToFile = 0;
if (-e $ARGV[0]) {
	open INF, $ARGV[0];
	if ($ARGV[1] ne "") {
		if (open OUTF, ">$ARGV[1]") {
			select OUTF;
			$OutToFile = 1;
		} else {
			## YOUR CHOICE: ERROR OR CONTINUE
		}
	}
	## DO STUFF
	## ALL print CALLS WILL GO TO THE DEFAULT OUTPUT (STDOUT)
	## UNLESS A VALID FILE WAS PASSED IN $ARGV[1]

	close INF;
	close OUTF if ($OutToFile);
}

Patrick Goetz wrote:
> 
> This isn't really a problem, since I know exactly how to get around it,
> but it is bothering me, and I'm thinking there must be a canonical
> solution to something this basic.
> 
> Suppose I want to have a script which takes a single input file and
> writes output to either a file or STDOUT based on the command line
> parameters given; i.e. if the user types
> 
>      prog infile xxx
> 
> prog reads data from infile and writes to xxx, and if the user types
> 
>      prog infile
> 
> the output is written to standard out, with anything else generating an
> die/error message.  Ignoring all extraneous features such as error
> checking for the moment, the obvious way to do this is something like
> this:
> 
>    if ( scalar(@ARGV) == 1 ) {
>      /*en (INPUT, "$ARGV[0*/;
>      *OUTPUT = *STDOUT;
>    }
>    if ( scalar(@ARGV) == 2 ) {
>      /*en (INPUT, "$ARGV[0*/;
>      /*en(OUTPUT, "$ARGV[1*/;
>    }
> <do some stuff>
>    print OUTPUT $stuff;
> 
> My problem is that I don't want to use a typeglob assignment to set
> OUTPUT to be the same as STDOUT for aesthetic reasons (and because
> I shouldn't have to - at least until I stand corrected).  However, it
> doesn't seem to be possible, because, as far as I can tell, I can't set
> OUTPUT to be a reference to STDOUT:
> 
>    $OUTPUT = *STDOUT;
> 
> because you can't open a filehandle as a reference:
> 
>    /*en( $OUTPUT, "$ARGV[1*/;
> 
> gives an error, so I can't just change all the OUTPUT's to $OUTPUT's in
> the script.
> 
> So, is there some way to not do a typeglob assignment, or is there a
> better, canonical way of solving this problem?

-- 
----------------------------------------------------------------
Jeff Helman                 Product Manager -- Internet Services
jhelman@wsb.com                    CCH Washington Service Bureau
----------------------------------------------------------------

99 little bugs in the code, 99 bugs in the code.
Fix one bug, compile again, 100 little bugs in the code.
100 little bugs in the code, 100 bugs in the code.
Fix one bug, compile again, 101 little bugs in the code...

----------------------------------------------------------------


------------------------------

Date: 19 May 2000 18:39:40 GMT
From: abigail@foad.org (Abigail)
Subject: Re: variable.pm
Message-Id: <slrn8ib2jc.prq.abigail@ucan.foad.org>

On 19 May 2000 02:46:17 GMT,
Intergalactic Denizen of Mystery <Tbone@pimpdaddy.com> wrote:
++ abigail@arena-i.com writes:
++ >Here's a way to use scalar variables without the needing a '$'.
++ 
++ Now how about overloading "." so you can look up attributes with
++ a.b.c etc?
++ 


I tried, but that isn't going to work in 5.6.0.

	#!/opt/perl/bin/perl

	no warnings;
	use overload '.' => \&dot;
	sub dot : lvalue {my ($obj, $method) = @_; $obj -> {$method};}

	my $o  = bless {} => "main";

	$o.foo = "bar";
	__END__


gives a compilation error:

         Can't modify concatenation (.) in scalar assignment at ./overloaded.pl
               line 9, near ""bar";"
         Execution of ./overloaded.pl aborted due to compilation errors.


I send a report to perlbug.



Abigail


------------------------------

Date: 19 May 2000 20:35:08 GMT
From: perl_phreak@yahoo.com (Gordon Clemmons)
Subject: Re: zen and the art of trolling [OT]
Message-Id: <8F3989870nospamblahcom@206.165.3.70>

xah@xahlee.org (Xah) wrote:
>The perl folks with their beads of little eyes, cannot see beyond their
>perl-perl-land.

You're an amusing little idiot.  Thanks for the laughs (at, not with).

-- Gordon



------------------------------

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V9 Issue 3102
**************************************


home help back first fref pref prev next nref lref last post