[22269] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4490 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 30 18:10:38 2003

Date: Thu, 30 Jan 2003 15:10:12 -0800 (PST)
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, 30 Jan 2003     Volume: 10 Number: 4490

Today's topics:
    Re: Regular expression and capturing <goldbb2@earthlink.net>
    Re: Regular expression and capturing (Hemuda)
    Re: Regular expression and capturing (Jay Tilton)
        Resolving network paths (Nishark)
    Re: Text extraction using patterns (Perl FAQ 6 not help (Tad McClellan)
    Re: Tie::IxHash <goldbb2@earthlink.net>
    Re: Tie::IxHash <tassilo.parseval@post.rwth-aachen.de>
        Why is this script sending 0 byte file attachments? (Tara)
    Re: Why is this script sending 0 byte file attachments? (Tad McClellan)
    Re: Why is this script sending 0 byte file attachments? Andrew Lee
    Re: Why is this script sending 0 byte file attachments? (Jay Tilton)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 30 Jan 2003 14:48:28 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Regular expression and capturing
Message-Id: <3E39818C.642D142F@earthlink.net>

Hemuda wrote:
> 
> Hello All,
> 
> I am reasonably good in perl. I am looking for a better way of doing
> the following. My actual problem is quite complex and I am just
> reproducing the problem in the following code. Any help or pointers in
> improving the efficiency is appreciated.
> 
> The string "s" is the start sequence (^s in regex) and "e" the end
> sequence (e$ in regex). I need to extract components a b c and d which
> are seperated by , and there can be a variable number of these. (Think
> of a b c d , as some sequence and I will replace corresponding regex).
> 
> @list = ( "sa,b,c,de" =~ m/^s(?:(\w),)+(\w)e$/ );
> 
> The above reqex matches the string but the capturing (@list) only
> receives (c, d). Capturing parenthesis for $1 gets overwritten and
> finally has just c. Currently to get all the components I have to
> store "a,b,c,d" in some variable and then have to use split which is
> costly.
> 
> How can I get the list ("a", "b", "c", "d") with just one regex match?

   @list = $string =~ /
      (?:    # Each 'letter' is preceded either by:
         ^s  # the beginning of the string, and an 's'
      |      # *or* after the last match
         \G(?!^) # but *not* at the beginning of the string.
      ) 
                
      (\w)      # get one letter.
      (?:,|e$)  # followed by , or by /e$/
   /xg;

> Is there any simple way to do it?

That depends on your definition of simple.  The above can be written as:

   @list = $string =~ /(?:^s|\G(?!^))(\w)(?:,|e$)/g;

But whether this is "simple" to you, I dunno.

> How to do it by embedding perl code witin the regular expression using
> (??{ code }) ? Examples to its usage is also appriciated.

I doubt that (??{ code }} is what you want.  And if it *is*, then you'll
have to figure it out yourself.

However, *if* I needed it as one expression, I would use two seperate
regular expressions:

   @list = map /\G(\w)(?:,|e$)/g, $string =~ /^s(.*)e$/;

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: 30 Jan 2003 14:10:53 -0800
From: hemuda@yahoogroups.com (Hemuda)
Subject: Re: Regular expression and capturing
Message-Id: <20d93d09.0301301410.1a370102@posting.google.com>

"Benjamin Goldberg" <goldbb2@earthlink.net> wrote in message 
  > > How can I get the list ("a", "b", "c", "d") with just one regex
match?
  > 
  >    @list = $string =~ /
  >       (?:    # Each 'letter' is preceded either by:
  >          ^s  # the beginning of the string, and an 's'
  >       |      # *or* after the last match
  >          \G(?!^) # but *not* at the beginning of the string.
  >       ) 
  >                 
  >       (\w)      # get one letter.
  >       (?:,|e$)  # followed by , or by /e$/
  >    /xg;
  > 

I really liked this. I had not used \G before. What if I have...

$string = "l,m sa,b,c,de x,y,z sl,m,n,o,pe %s% s0,1,2,3,4,5,6e %e%
h,i"

So there is %s% or %e% in the string. And regex of type "s(\w,)+\we"
is what would match my intent. I am using /g modifier so the regular
expression matches
sa,b,c,de    
sl,m,n,o,pe    
and
s0,1,2,3,4,5,6e 

In my original mail I wanted to say (?{ code }) and not (??{ code }).

This is what I tried...

@m = ();
$_ = $string;
m/s(?:(\w),(?{ local @m = (@m, $1); }))+(\w)e(?{ @list = @m})/

In my debugger the above expression printed...

via UNIVERSAL: VERSION
via UNIVERSAL: can
via UNIVERSAL: isa

Thanks,
Hemant


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

Date: Thu, 30 Jan 2003 22:45:45 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Regular expression and capturing
Message-Id: <3e39aa83.385356728@news.erols.com>

hemuda@yahoogroups.com (Hemuda) wrote:
: hemuda@yahoogroups.com (Hemuda) wrote
: > The string "s" is the start sequence (^s in regex) and "e" the end
: > sequence (e$ in regex). I need to extract components a b c and d which
: > are seperated by , and there can be a variable number of these.

: I want a signle regular expression.

Your reasons for wanting this are not sound.

: It can probably be done with (?{code}), with proper code.

With proper code, anything can be done with (?{code}).  It's code!

    m((?{
        # Janek's solution
        @list = split /,/, substr "sa,b,c,de", 1, -1; 
    }));
 
: Using two regular expressions, or split would be costly, 

Costly how?  Costly to whom?

Who paid for the day already lost to this snipe hunt?  Could that
investment in development time possibly be justified by shaving
microseconds from the run time?

This is premature optimization.



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

Date: 30 Jan 2003 13:52:49 -0800
From: john_grisham81@yahoo.com (Nishark)
Subject: Resolving network paths
Message-Id: <d2bd49b7.0301301352.5ce84fef@posting.google.com>

I'm trying to store the full network path of a file in a shared
directory network into a database, so that other users using the same
database can call up this document from anywhere in the network.

for example, say,
Mapped location on local system: L:\Subdirectory\Disaster.doc
Network location: \\NetworkFileServer\ITDATA\Disaster.doc

I have a form where the user browses the local file system to
'upload'. My aim is to get this location and map it to the
corresponding network path location. Then store the network path in
the database.
in this case, if a user decides to upload 'Disaster.doc' from his/her
L: drive, the network location of the file i.e.
"\\NetworkFileServer\ITDATA\Disaster.doc" is stored in the database.

How do I get this corresponding network location?

I'll be using JSP/Javascript, but dont hesitate to present solutions
using anything else.

Thanks...


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

Date: Thu, 30 Jan 2003 13:02:30 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Text extraction using patterns (Perl FAQ 6 not helping!!)
Message-Id: <slrnb3itm6.kam.tadmc@magna.augustmail.com>

Stephen Adam <00056312@brookes.ac.uk> wrote:

> I am trying to extract some information from a dynamically created
> HTML page. The information I want always starts with "< class=g>"
                                                        ^^^^^^^^^^

That is not legal HTML...

(actually it is legal, but it is content, not markup)


> ends with "</a> </font>".

[snip]

> our $htmlpage = "random stuff zz here is some stuff I want xx some
> more random stuff zz and here is some other stuff I want xx and yet
> more random stuff";


You should get an empty array from that data, as it does not
contain any of the things you said you are looking for.

If you can't be troubled to provide realistic data, it is unlikely
that you will find a volunteer to both make data, and then answer
your question. You have not made in easy to answer your question.


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


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

Date: Thu, 30 Jan 2003 14:33:44 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Tie::IxHash
Message-Id: <3E397E18.40F3A30D@earthlink.net>

"Tassilo v. Parseval" wrote:
[snip]
> then %hash is no longer tied since the hash is passed flattened to a
> list and rebuilt within func(). You have to pass a referece:
> 
>     func(\%var);
> 
>     sub func {
>         my ($ref) = @_;
>         my %hash  = \%ref;
>         # %hash still tied

*blink blink*

Umm, use strict and warnings :P

Now, if you'd done either:

   sub func {
      my ($ref) = @_;
      our %hash;
      local *hash = \%$ref;
      ... stuff ...
   }

or:

   use Devel::LexAlias; # from CPAN.
   sub func {
      my ($ref) = @_;
      my %hash; lexalias(0, '%hash', \%$ref);
      ... stuff ...
   }

Then certainly %hash would indeed be tied.

Of course, the safer, more sensible thing to do, is:
   sub func {
      my ($ref) = @_; # %$ref is still tied.
      ... stuff ...
   }

-- 
$..='(?:(?{local$^C=$^C|'.(1<<$_).'})|)'for+a..4;
$..='(?{print+substr"\n !,$^C,1 if $^C<26})(?!)';
$.=~s'!'haktrsreltanPJ,r  coeueh"';BEGIN{${"\cH"}
|=(1<<21)}""=~$.;qw(Just another Perl hacker,\n);


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

Date: 30 Jan 2003 22:23:19 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Tie::IxHash
Message-Id: <b1c8kn$2hf$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Benjamin Goldberg:

> "Tassilo v. Parseval" wrote:
> [snip]
>> then %hash is no longer tied since the hash is passed flattened to a
>> list and rebuilt within func(). You have to pass a referece:
>> 
>>     func(\%var);
>> 
>>     sub func {
>>         my ($ref) = @_;
>>         my %hash  = \%ref;
>>         # %hash still tied
> 
> *blink blink*
> 
> Umm, use strict and warnings :P

Err, and I really needed a few secs to spot this typo. But since I
always use strictures and warnings in any actual code (save for a few
exceptins - just in case Abigail informs me about reimplementing
Exporter) the above wouldn't have slipped through unnoticed.

I wonder whether -w and strict has made me a more careless programmer. I
don't spend much time on 'correctness' (with respect to typos) in my
code any longer because it's so convenient to have Perl spot it for me.

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: 30 Jan 2003 11:42:02 -0800
From: tararse@hotmail.com (Tara)
Subject: Why is this script sending 0 byte file attachments?
Message-Id: <27eb673f.0301301142.5bc9612d@posting.google.com>

Hello, this script is passing the uploaded files as 0 bytes... can
someone see what I am doing wrong here?  Perhaps it's in my encoding?

Thanks and kind regards,

Tara

@@@@ START OF .PL SCRIPT

#!/usr/local/bin/perl

$|++;
# _________________________________________________________

use CGI;

$query=new CGI;

print $loc = "Location:/confirm.send.htm\n";

print "Content-type: text/html\n\n";

$dir_to_store="/tmp";
$no_of_files=0;
@list=();

$email_recipient = 'tararse@hotmail.com;
$subject = "Subject Line One";
$Subject1 = "Subject Line Two";
$Message1 = "Message";

&mailAttachment;
&confirmation;

# _________________________________________________________    
sub mailAttachment {

$boundary="Message-Boundary-19990614";
$mailprog ="/usr/lib/sendmail";

foreach ($query->param){
    if ($query->param($_)){     ##     The user entered some value!!
         if (/^file_attached_[A-Z]$/){     ##     The uploaded file!!
              ++$no_of_files;
              push(@list,$_);


         }else{     ##     One of the other items (Sender's Name,
Sender's Email, etc...).
              ##     Store the value in a variable.
              ##     The name of the variable is the same as the HTML
form element name
              ##     Eg if the HTML element is called name_sender, the
PERL
              ##     variable will be called $name_sender.
              $$_=$query->param($_);

         }
    }
}

 
    open(MAIL, "| $mailprog -t ");
    print MAIL "To: $name_recipient <$email_recipient>\n";
    print MAIL "From: $name_sender <$email_sender>\n";
    print MAIL "CC: $cc\n";
    print MAIL "BCC: $bcc\n";
    print MAIL "Subject: $subject\n";     
    print MAIL "MIME-Version: 1.0\n";
    print MAIL "Content-type: Multipart/1;\n";
    print MAIL "\tboundary=$boundary\n";
    print MAIL "\n";
    print MAIL "\n";
    print MAIL "--$boundary\n";

    foreach (@list){
         $uploaded_file=$query->param($_);

         $tmp_uploaded_file=$query->param($_);
         $tmp_uploaded_file=~ s/\\/\//g;
         @tmp_uploaded_file=split(/\//,$tmp_uploaded_file);

         $filename = $tmp_uploaded_file[$#tmp_uploaded_file];

         $content="";
         while($bytesread=read($uploaded_file,$data,1024)){
              $size+=$bytesread;
              $content.=$data;
         } 
         close($uploaded_file);
         
         $size=0;

         ##  Print the header for that attachment.
         print MAIL "Content-type: application/octet-stream;
name=\"$filename\"; type=Unknown\n";
         print MAIL "Content-transfer-encoding: BASE64\n";
         print MAIL "Content-disposition: attachment\n";
         print MAIL "\n";

         $content= encode_base64($content);  ##  Use base64 for
encoding the contents

         ##  Attach the file!!
         print MAIL $content;
         print MAIL "\n";
         print MAIL "--$boundary\n";
    }

    ##     Add the text part of the message
    print "Content-type: text/html\n\n";
    print MAIL "Content-type: text/plain; charset=US-ASCII\n";
    print MAIL "Content-description: Mail message body\n";
    print MAIL "Content-transfer-encoding: 7BIT\n";
    print MAIL "\n";
    print MAIL "First Name: $first_name\n";
    print MAIL "Last Name: $last_name\n";
    print MAIL "Email: $email_sender\n\n";     
    print MAIL "Phone Number: $phone_number\n";
    print MAIL "Address Line 1: $address_1\n";
    print MAIL "Address Line 2: $address_2\n";
    print MAIL "Address Line 3: $address_3\n";
    print MAIL "City: $city\n";
    print MAIL "State: $state\n";
    print MAIL "Province: $province\n";
    print MAIL "Country: $country_name\n";
    print MAIL "Zip/Postal Code: $zippost\n";
    print MAIL "--$boundary\n";

    ##  Send out the contents!!
    print MAIL "Message/Comments\n$main_message";
    print MAIL "\n";
    print MAIL "--$boundary--\n";
    close(MAIL);

}
# _________________________________________________________

sub confirmation {

open(MAIL, "| $mailprog -t ");
print MAIL "To: $email_sender\n";
print MAIL "From: $email_recipient\n";
print MAIL "Subject: $Subject1\n";

print MAIL "$Message1\n\n";

close (MAIL);

}
# _________________________________________________________

    print "$loc\n\n";
    

# _________________________________________________________

sub encode_base64 ($;$){
    my $res = "";
    my $eol = $_[1];
    $eol = "\n" unless defined $eol;
    pos($_[0]) = 0;                          # ensure start at the
beginning
    while ($_[0] =~ /(.{1,45})/gs) {
         $res .= substr(pack('u', $1), 1);
         chop($res);
    }
    $res =~ tr|` -_|AA-Za-z0-9+/|;               # `# help emacs
    # fix padding at the end
    my $padding = (3 - length($_[0]) % 3) % 3;
    $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
    # break encoded string into lines of no more than 76 characters
each
    if (length $eol) {
         $res =~ s/(.{1,76})/$1$eol/g;
    }
    $res;
}


@@@@ HTML TEST SNIPPET

<html>
<head>
     <title>Test Upload</title>
</head>
<body>
<FORM ACTION="./test_upload.pl" METHOD="post">

<table>
<tr>
  <td width="125" height="30">File 1</td>
  <td height="30" width="306"><input type="file"
name="file_attached_A"</td>
</tr>
<tr>
  <td width="125" height="30">File 2</td>
  <td height="30" width="306"><input type="file"
name="file_attached_B"</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit Files" name="submit">
</td>
</tr>
</table>
</form>
</body><script> window.open=NS_ActualOpen; </script>
<script> window.open=NS_ActualOpen; </script>

</html>


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

Date: Thu, 30 Jan 2003 15:15:26 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Why is this script sending 0 byte file attachments?
Message-Id: <slrnb3j5fe.kjj.tadmc@magna.augustmail.com>

Tara <tararse@hotmail.com> wrote:

> can
> someone see what I am doing wrong here?


> @@@@ START OF .PL SCRIPT
> 
> #!/usr/local/bin/perl


The start of the program has no warnings or strictures:

   use strict;
   use warnings;

So the start of the program is the end of my code review.

Good luck with it though.



[snip 200 line program]

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


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

Date: Thu, 30 Jan 2003 16:44:15 -0500
From: Andrew Lee
Subject: Re: Why is this script sending 0 byte file attachments?
Message-Id: <lv6j3vc7u3n3r463eahleiujspfurh3cba@4ax.com>

On 30 Jan 2003 11:42:02 -0800, tararse@hotmail.com (Tara) wrote:

>Hello, this script is passing the uploaded files as 0 bytes... can
>someone see what I am doing wrong here? 

You are posting a 200 line cgi to a newsgroup and asking people to debug
it ... some of us have lives, you know.

Read the posting guidelines, please.  And note -- c.l.p.misc is not your
personal help desk -- you are expected to do your homework .. in your
case turn on warnings and strictures and isolate that part of code (or a
smaller example) that demonstrates your problem.


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

Date: Thu, 30 Jan 2003 22:51:03 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Why is this script sending 0 byte file attachments?
Message-Id: <3e39abd8.385697307@news.erols.com>

tararse@hotmail.com (Tara) wrote:

: Hello, this script is passing the uploaded files as 0 bytes... can
: someone see what I am doing wrong here?  Perhaps it's in my encoding?

[snip]

: $email_recipient = 'tararse@hotmail.com;

[snip]

Or perhaps it's just a stinking syntax error.

Nothing like asking people to do a job the computer will do, is there?



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

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.  

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


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