[24362] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6551 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue May 11 06:10:40 2004

Date: Tue, 11 May 2004 03:10:09 -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           Tue, 11 May 2004     Volume: 10 Number: 6551

Today's topics:
        Sysread in perl (Tony)
    Re: Sysread in perl <ThomasKratz@REMOVEwebCAPS.de>
    Re: Sysread in perl <gnari@simnet.is>
    Re: upload file with perl cgi <sean_berry@cox.net>
        Using match variables ($1, $2 ...) as variables. <parimi@none.nowhere.com>
    Re: Using match variables ($1, $2 ...) as variables. <invalid-email@rochester.rr.com>
    Re: Using match variables ($1, $2 ...) as variables. <tassilo.parseval@rwth-aachen.de>
    Re: Using match variables ($1, $2 ...) as variables. <gnari@simnet.is>
    Re: Very urgent: plz someone answer <gnari@simnet.is>
    Re: Very urgent: plz someone answer <xxala_qumsiehxx@xxyahooxx.com>
    Re: Very urgent: plz someone answer <gnari@simnet.is>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 11 May 2004 01:39:14 -0700
From: ts@mplhost.com (Tony)
Subject: Sysread in perl
Message-Id: <2a295a23.0405110039.375ab6ac@posting.google.com>

Hi, 

I am trying to read the last 500 bytes of a file. I'm trying to use
sysread command, I have read the docs for it on cpan and it seems the
right thing to use.
My only problem is when i spcify a negative offset it fails with the
following error  "Offset outside string at test.pl line 30.".

here is the line it is complaing about 

         sysread(F,$string,500,-500);

I have tried escaping the minus sign , putting it into a var and using
eval and they all come back with the same error.

Heres the paragraph from cpans docs

"An OFFSET may be specified to place the read data at some place in
the string other than the beginning. A negative OFFSET specifies
placement at that many characters counting backwards from the end of
the string. A positive OFFSET greater than the length of SCALAR
results in the string being padded to the required size with "\0"
bytes before the result of the read is appended."

should this work or is it just me reading the distructions wrong.


thanks


Tony


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

Date: Tue, 11 May 2004 11:10:44 +0200
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: Sysread in perl
Message-Id: <40a09a15.0@juno.wiesbaden.netsurf.de>

Tony wrote:
> Hi, 
> 
> I am trying to read the last 500 bytes of a file. I'm trying to use
> sysread command, I have read the docs for it on cpan and it seems the
> right thing to use.
> My only problem is when i spcify a negative offset it fails with the
> following error  "Offset outside string at test.pl line 30.".
> 
> here is the line it is complaing about 
> 
>          sysread(F,$string,500,-500);
> 
> I have tried escaping the minus sign , putting it into a var and using
> eval and they all come back with the same error.
> 
> Heres the paragraph from cpans docs
> 
> "An OFFSET may be specified to place the read data at some place in
> the string other than the beginning. A negative OFFSET specifies
> placement at that many characters counting backwards from the end of
> the string. A positive OFFSET greater than the length of SCALAR
> results in the string being padded to the required size with "\0"
> bytes before the result of the read is appended."
> 
> should this work or is it just me reading the distructions wrong.

It's you ;-) You are *reading* from the filehandle ( called F in your 
example) and *writing* to $string. The offset specifies where in $string 
you want to put the number of bytes specified in the 3rd parameter, not 
where you start reading from the file.

If you want to read from the end of a file look at

    perldoc -f seek
    perldoc -q tail

or the modules

    File::Tail
    File::ReadBackwards

on CPAN

Thomas

-- 
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</  #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..


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

Date: Tue, 11 May 2004 09:09:35 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: Sysread in perl
Message-Id: <c7q55f$23p$1@news.simnet.is>

"Tony" <ts@mplhost.com> wrote in message
news:2a295a23.0405110039.375ab6ac@posting.google.com...
> Hi,
>
> I am trying to read the last 500 bytes of a file. I'm trying to use
> sysread command, I have read the docs for it on cpan and it seems the
> right thing to use.
> My only problem is when i spcify a negative offset it fails with the
> following error  "Offset outside string at test.pl line 30.".

this error message should have been a hint

>
> Heres the paragraph from cpans docs
>
> "An OFFSET may be specified to place the read data at some place in
> the string other than the beginning. A negative OFFSET specifies
> placement at that many characters counting backwards from the end of
> the string. A positive OFFSET greater than the length of SCALAR
> results in the string being padded to the required size with "\0"
> bytes before the result of the read is appended."

notice how it says 'string' but not 'file' everywhere

>
> should this work or is it just me reading the distructions wrong.

a) if you expect sysread with OFFSET -500 to read last 500
bytes of file, then you are reading the 'distructions' wrong,
and should lookup sysseek

b) if you expect it to read into the last 500 bytes of your
string, then you need to make sure the string is at lead 500
bytes to start with, as the \0 padding only applies to
positive OFFSETS

gnari






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

Date: Mon, 10 May 2004 22:01:25 -0700
From: "Sean Berry" <sean_berry@cox.net>
Subject: Re: upload file with perl cgi
Message-Id: <56Znc.105709$Jy3.99607@fed1read03>

I do a file upload in part of a script I wrote.

This is the code for the input field:
<input type="file" name="filename" size=40 maxlength=64 accept="pdf/*">

Then, in an upload subroutine I do something like this:
$pdfdir = "/admin/ppv_pdfs";
$x = file_upload($filename, $pdfdir);

You don't need to do "$x =" if all you do is upload the file.  But, if you
can use the return value then it could be helpful as well.





"Sokar" <jmccloughlin@hotmail.com> wrote in message
news:34628238.0405100304.46fa9a97@posting.google.com...
> I am designing a new feature to a services web page for the user to upload
> screen dumps of problems onto my server for inspection and maybe further
> use.
>
> I have investigated the <input TYPE='file' NAME='pic' size='15'> for use
in
> the html part.
>
> The problem comes when I get to the perl cgi part of the code, I am havin
> problems saving the .jpg onto my server.
>
> Can anyone provide a little help on how to upload files onto the server
via
> a web interface?
>
> Thanks
>
> John




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

Date: Mon, 10 May 2004 18:44:53 -0700
From: Ravi Parimi <parimi@none.nowhere.com>
Subject: Using match variables ($1, $2 ...) as variables.
Message-Id: <Pine.GSO.4.58.0405101802580.18191@shellfish.ece.arizona.edu>

Hi,
	I have a string that gets generated on the fly. Based on the
number of times a particular word appears in this string, I generate my
matching pattern. As an example..

$string = "<Data><Size>12</Size>one string<Size>100</Size>Another
           string</Data>";

Now, based on the number of <Size> tags in the string, I create a regular
expression such as

my $count = $string =~ s#<Size>##gs;
my $str = "";
for ($i = 1;$i <= $count;++$count) {
	 $str .= "<Size>(\\d+)</Size>(.*)";
}

And now I wish to extract all odd-numbered match variables from the
matching($1, $3, $5 etc..)

$string =~ /$str/; # Match always succeeds as I know what's in $string

In a for loop, I wish to check if each of the $1,$2,$3...variables are
defined, and am not quite sure how to do that.


Usage of ${$i} is incorrect and doesnt make sense. Can someone suggest a
method by which I can find out if $1, $2, $3 ..are defined?


As an alternative, I have tried XML Parser to parse the above string but
had no luck with it.. Any help is appreciated.

Thanks,
--ravi



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

Date: Tue, 11 May 2004 03:14:57 GMT
From: Bob Walton <invalid-email@rochester.rr.com>
Subject: Re: Using match variables ($1, $2 ...) as variables.
Message-Id: <40A0452F.6070501@rochester.rr.com>

Ravi Parimi wrote:

 ...
> 	I have a string that gets generated on the fly. Based on the
> number of times a particular word appears in this string, I generate my
> matching pattern. As an example..
> 
> $string = "<Data><Size>12</Size>one string<Size>100</Size>Another
>            string</Data>";
> 
> Now, based on the number of <Size> tags in the string, I create a regular
> expression such as
> 
> my $count = $string =~ s#<Size>##gs;


Why do you have the "s" modifier on this regexp?  You don't use the . 
metacharacter in it, and the "s" modifiers only purpose is to change the 
behavior of the . metacharacter.


> my $str = "";
> for ($i = 1;$i <= $count;++$count) {


Hmmmmm...if $count is, for example, 3, then this will loop forever 
(well, until overflow, anyway), as $i will remain 1 and count will 
increment to 4, 5, 6, etc.  What is your real code?


> 	 $str .= "<Size>(\\d+)</Size>(.*)";
> }
> 
> And now I wish to extract all odd-numbered match variables from the
> matching($1, $3, $5 etc..)
> 
> $string =~ /$str/; # Match always succeeds as I know what's in $string
> 
> In a for loop, I wish to check if each of the $1,$2,$3...variables are
> defined, and am not quite sure how to do that.
> 
> 
> Usage of ${$i} is incorrect and doesnt make sense. Can someone suggest a
> method by which I can find out if $1, $2, $3 ..are defined?
> 


Check out:

    perldoc perlvar

in particular the part where it talks about the @- and @+ variables. 
These give you programmatic access to the $1, $2, etc variables, via 
substr() and the variable you matched the pattern against.


> 
> As an alternative, I have tried XML Parser to parse the above string but
> had no luck with it.. Any help is appreciated.
> 
 ...
> --ravi

-- 
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl



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

Date: Tue, 11 May 2004 06:32:48 +0200
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Using match variables ($1, $2 ...) as variables.
Message-Id: <2gb3biFmq15U1@uni-berlin.de>

Also sprach Ravi Parimi:

> 	I have a string that gets generated on the fly. Based on the
> number of times a particular word appears in this string, I generate my
> matching pattern. As an example..
> 
> $string = "<Data><Size>12</Size>one string<Size>100</Size>Another
>            string</Data>";
> 
> Now, based on the number of <Size> tags in the string, I create a regular
> expression such as
> 
> my $count = $string =~ s#<Size>##gs;
> my $str = "";
> for ($i = 1;$i <= $count;++$count) {
> 	 $str .= "<Size>(\\d+)</Size>(.*)";
> }
> 
> And now I wish to extract all odd-numbered match variables from the
> matching($1, $3, $5 etc..)
> 
> $string =~ /$str/; # Match always succeeds as I know what's in $string
> 
> In a for loop, I wish to check if each of the $1,$2,$3...variables are
> defined, and am not quite sure how to do that.

A more natural solution would be changing the pattern accordingly. You
always add two pairs of capturing parens. What is the second pair for?

Other than that, do the pattern match in list context and extract the
uneven elements from the array:

    my @matches = $string =~ /$str/;
    @matches = map { $_ % 2 ? $matches[$_] : () } 0 .. $#matches;

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Tue, 11 May 2004 08:12:42 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: Using match variables ($1, $2 ...) as variables.
Message-Id: <c7q1qp$1na$1@news.simnet.is>

"Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de> wrote in message
news:2gb3biFmq15U1@uni-berlin.de...
> Also sprach Ravi Parimi:
>
> > I have a string that gets generated on the fly. Based on the
> > number of times a particular word appears in this string, I generate my
> > matching pattern. As an example..
> >
> > $string = "<Data><Size>12</Size>one string<Size>100</Size>Another
> >            string</Data>";
> >
> > my $count = $string =~ s#<Size>##gs;
> > my $str = "";
> > for ($i = 1;$i <= $count;++$count) {
> > $str .= "<Size>(\\d+)</Size>(.*)";
> > }

>     my @matches = $string =~ /$str/;
>     @matches = map { $_ % 2 ? $matches[$_] : () } 0 .. $#matches;

exept that the greedy (.*) will spoin the match it there are more that
 2 pairs of the tags.

I would skip the pattern building and just do (untested):
      my @matches = $string =~ /<Size>(\d+)</Size>([^<]*)/gs;
and then process the array similarly to Tassilo.

gnari






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

Date: Mon, 10 May 2004 23:21:18 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: Very urgent: plz someone answer
Message-Id: <c7p2mb$thk$1@news.simnet.is>

"Neo" <me4uwithluv@yahoo.com> wrote in message
news:639fa221.0405092052.4b4c36a7@posting.google.com...
> I have a question:
>
> I am asked in an interview:
>
> How is an array interpolated in Perl.

what kind of interview is this, that this question is so urgent,
that you have no time to look at the docs, or even make a few
simple tests, but still not so urgent that you can wait for your
question and the answers to propagate through usenet?

your question is not really specific enough.
are you asking about underlying technology, or what
happens when you print "@x", or what special variables
are involved, or how to do it?

please answer these urgent questions asap.

gnari






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

Date: Tue, 11 May 2004 04:51:36 GMT
From: Ala Qumsieh <xxala_qumsiehxx@xxyahooxx.com>
Subject: Re: Very urgent: plz someone answer
Message-Id: <sZYnc.47131$Ty1.7955@newssvr29.news.prodigy.com>

gnari wrote:
> what kind of interview is this, that this question is so urgent,
> that you have no time to look at the docs, or even make a few
> simple tests, but still not so urgent that you can wait for your
> question and the answers to propagate through usenet?

Perhaps he has a web-enabled cell phone and was able to post this 
message while taking an interview examination?

--Ala



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

Date: Tue, 11 May 2004 07:58:52 -0000
From: "gnari" <gnari@simnet.is>
Subject: Re: Very urgent: plz someone answer
Message-Id: <c7q10q$1hl$1@news.simnet.is>

"Ala Qumsieh" <xxala_qumsiehxx@xxyahooxx.com> wrote in message
news:sZYnc.47131$Ty1.7955@newssvr29.news.prodigy.com...
> gnari wrote:
> > what kind of interview is this, that this question is so urgent,
> > that you have no time to look at the docs, or even make a few
> > simple tests, but still not so urgent that you can wait for your
> > question and the answers to propagate through usenet?
>
> Perhaps he has a web-enabled cell phone and was able to post this
> message while taking an interview examination?

ahh. that's it.

gnari






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

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 6551
***************************************


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