[26286] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8468 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Sep 29 09:05:28 2005

Date: Thu, 29 Sep 2005 06:05:05 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 29 Sep 2005     Volume: 10 Number: 8468

Today's topics:
        ARGV interpretation in s/// <goth1938@hotmail.com>
    Re: ARGV interpretation in s/// <tadmc@augustmail.com>
    Re: ARGV interpretation in s/// <nobull@mail.com>
        Filehandles <guenther.sohler@newlogic.com>
    Re: Filehandles <tassilo.von.parseval@rwth-aachen.de>
    Re: Filehandles (Anno Siegel)
    Re: Filehandles <nobull@mail.com>
    Re: Filehandles <tadmc@augustmail.com>
    Re: Filehandles <nobull@mail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 29 Sep 2005 12:01:00 +0800
From: "Just in" <goth1938@hotmail.com>
Subject: ARGV interpretation in s///
Message-Id: <dhfott$n44$1@news01.intel.com>

I'm trying to write regexp search and replace strings on the command line
and have the program I'm calling use them in a substitution. Problem is that
the $[0-9] variables are not being interpreted the way I want them to be.

Heres what I am doing on my Linux box:-

<OUTPUT>
%> try.pl -f to.txt -s 'add (\w+) minus' -r 'FOO$1BAR'
FOO$1BAR
FOO$1BAR
%>
</OUTPUT>

<CODE>
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;

# Variable declaration
my $Search =  my $Replace = my $File = '';

# Process @ARGV
getopts('f:hr:s:');
our($opt_f, $opt_r, $opt_s);

$opt_f ? $File = $opt_f : &DisplayHelp();
$opt_r ? $Replace = $opt_r : &DisplayHelp();
$opt_s ? $Search = $opt_s : &DisplayHelp();

open(FILE, $File) or die "Can't open $File:- $!";
while(<FILE>)
{
        s/$Search/$Replace/;
        print;
}
close FILE;

sub DisplayHelp
{
        print "To be defined\n";
        exit 0;
}
</CODE>

How can I achieve the desired interpretation of $1? Single quotes and double
quotes don't help, nor does qr//

Cheers

Just in




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

Date: Thu, 29 Sep 2005 07:22:56 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: ARGV interpretation in s///
Message-Id: <slrndjnn50.61u.tadmc@magna.augustmail.com>

Just in <goth1938@hotmail.com> wrote:

> Problem is that
> the $[0-9] variables are not being interpreted the way I want them to be.



>         s/$Search/$Replace/;

> How can I achieve the desired interpretation of $1?


   $Replace = qq("$Replace");
   s/$Search/$Replace/ee;


See also:

   perldoc -q expand

       How can I expand variables in text strings?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Thu, 29 Sep 2005 13:25:26 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: ARGV interpretation in s///
Message-Id: <dhgmfm$r0v$1@redhat2.bham.ac.uk>

Just in wrote:

> I'm trying to write regexp search and replace strings on the command line
> and have the program I'm calling use them in a substitution. Problem is that
> the $[0-9] variables are not being interpreted the way I want them to be.

Please see my "History of a FAQ" lightning talk about this question...

http://birmingham.pm.org/talks/faq/Hello.html



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

Date: Thu, 29 Sep 2005 10:23:48 +0200
From: Guenther Sohler <guenther.sohler@newlogic.com>
Subject: Filehandles
Message-Id: <pan.2005.09.29.08.23.47.792918@newlogic.com>

Hallo,

I am writing a small parser in perl, which should also be able to
include files.
if a include statement happens,
the prg shall put the current FH onto the stack and open a new file with
the same FH.

How can I do this as filehandles are special in perl ?



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

Date: Thu, 29 Sep 2005 10:41:43 +0200
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: Filehandles
Message-Id: <slrndjna67.1ag.tassilo.von.parseval@localhost.localdomain>

Also sprach Guenther Sohler:

> I am writing a small parser in perl, which should also be able to
> include files.
> if a include statement happens,
> the prg shall put the current FH onto the stack and open a new file with
> the same FH.
>
> How can I do this as filehandles are special in perl ?

Just pretend that they aren't special. Filehandles no longer need to be
barewords:

    open my $fh, "<", $file or die "$file: $!";
    push @stack, $fh;

    ...

    # and then later
    my $element = pop @stack;
    if (ref($element) eq 'GLOB') {  # is it a filehandle?
        while (<$element>) {            # then: read file
            ...
        }
    }

Note that your terminology is a bit sloppy: If it's a filehandle then
the underlying file has already been opened and you cannot really "open
a new file with the same FH".

Tassilo
-- 
use bigint;
$n=71423350343770280161397026330337371139054411854220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);


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

Date: 29 Sep 2005 08:59:53 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Filehandles
Message-Id: <dhgae9$dj$1@mamenchi.zrz.TU-Berlin.DE>

Guenther Sohler  <guenther.sohler@newlogic.com> wrote in comp.lang.perl.misc:
> Hallo,
> 
> I am writing a small parser in perl, which should also be able to
> include files.
> if a include statement happens,
> the prg shall put the current FH onto the stack and open a new file with
> the same FH.
> 
> How can I do this as filehandles are special in perl ?

You use what is known as a lexical filehandle:

    open my $fh, $file;

These are (slightly simplified) lexical variables that contain a reference
to an anonymous filehandle.  The filehandle itself is still global.

Also search CPAN for the keyword "include".  What comes up looks like
your problem has been dealt with before.

Anno
-- 
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on 
"show options" at the top of the article, then click on the 
"Reply" at the bottom of the article headers.


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

Date: Thu, 29 Sep 2005 13:18:46 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Filehandles
Message-Id: <dhgm36$qq7$1@redhat2.bham.ac.uk>

Tassilo v. Parseval wrote:

> Note that your terminology is a bit sloppy: If it's a filehandle then
> the underlying file has already been opened and you cannot really "open
> a new file with the same FH".

Er, no.  Perl filehandles (aka "IO thingys") can exist in a closed state 
and a new file can be associated with an existinh file handle with open().



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

Date: Thu, 29 Sep 2005 07:01:34 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Filehandles
Message-Id: <slrndjnlsu.61u.tadmc@magna.augustmail.com>

Guenther Sohler <guenther.sohler@newlogic.com> wrote:

> put the current FH onto the stack

> How can I do this as filehandles are special in perl ?


   perldoc -q filehandle


       How can I make a filehandle local to a subroutine?  How do I
       pass filehandles between subroutines?  How do I make an array
       of filehandles?


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Thu, 29 Sep 2005 13:21:05 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: Filehandles
Message-Id: <dhgm7i$qq7$2@redhat2.bham.ac.uk>



Brian McCauley wrote:

> Tassilo v. Parseval wrote:
> 
>> Note that your terminology is a bit sloppy: If it's a filehandle then
>> the underlying file has already been opened and you cannot really "open
>> a new file with the same FH".
> 
> 
> Er, no.

Ignore that.

I now realise you were drawing a destinction between "file handle" and 
"IO handle".  When a filehandle is closed it just becomes a closed IO 
handle.



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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V10 Issue 8468
***************************************


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