[22133] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4355 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jan 7 00:10:41 2003

Date: Mon, 6 Jan 2003 21:10:10 -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           Mon, 6 Jan 2003     Volume: 10 Number: 4355

Today's topics:
        Q: Easy regular expression available for this... (smallya)
    Re: Q: Easy regular expression available for this... (Sam Holden)
    Re: Q: Easy regular expression available for this... <goldbb2@earthlink.net>
        Recreating directory hierarchy (Andrew Luke NESBIT)
    Re: Recreating directory hierarchy <goldbb2@earthlink.net>
    Re: Recreating directory hierarchy <jurgenex@hotmail.com>
    Re: Running CGI scripts offline <jurgenex@hotmail.com>
        Shortcut for lvalue issue? <me@quietplaceiwithnospam.com>
    Re: Shortcut for lvalue issue? <goldbb2@earthlink.net>
    Re: Shortcut for lvalue issue? <krahnj@acm.org>
    Re: Shortcut for lvalue issue? <me@quietplaceiwithnospam.com>
    Re: top-posting (was Re: regexp question) <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 6 Jan 2003 17:34:06 -0800
From: sxm1972@yahoo.com (smallya)
Subject: Q: Easy regular expression available for this...
Message-Id: <11bbce7e.0301061734.14f8ecb0@posting.google.com>

I have text which is of the form:
{sometext}{more text {even more text}} {{yet more text}{blah blah}}
which I want to break into:
{sometext}
{more text {even more text}} 
{{yet more text}{blah blah}}

I want to break the line into parts enclosed by only the outermost
braces. Is there an easy way to do it with regular expressions?

TIA
SM


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

Date: 7 Jan 2003 02:20:27 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: Q: Easy regular expression available for this...
Message-Id: <slrnb1keba.m11.sholden@flexal.cs.usyd.edu.au>

On 6 Jan 2003 17:34:06 -0800, smallya <sxm1972@yahoo.com> wrote:
> I have text which is of the form:
> {sometext}{more text {even more text}} {{yet more text}{blah blah}}
> which I want to break into:
> {sometext}
> {more text {even more text}} 
> {{yet more text}{blah blah}}
> 
> I want to break the line into parts enclosed by only the outermost
> braces. Is there an easy way to do it with regular expressions?

You could try reading the documentation that comes with perl
(assumming you have a relatively recent perl):

The perlre documentation (readable with "perldoc perlre" or via some
vendor specific system (probably web pages)) has a pattern for matching
a parenthesized group. Wrap that in () to capture it and use /g in a list
content, and you will get what you want (after changing it to look for {}
instead of ().

In fact here it is:

# slightly modified copy of example in perlre documentation
$re = qr{
           ({
           (?:
              (?> [^{}]+ )    # Non-{}s without backtracking
              |
              (??{ $re })     # Group with matching {}s
           )*
           })
        }x;

$text="{sometext}{more text {even more text}} {{yet more text}{blah blah}}";

@groups = $text=~/$re/g;
print join "\n", @groups,'';

I wouldn't call that 'easy'. I suggest you do read the docs and not just
cur-n-paste that since it contains wonderful words like:

    WARNING: This extended regular expression feature is considered
    highly experimental, and may be changed or deleted without notice.


There's also the Regexp::Common::balanced module (part of Regexp::Common),
but I don't know if it allows extraction of the groups?

Or you could just count {s and }s manually to construct the strings. Or
if there's a very low depth limit a simple regular expression can be
constructed withous uding 'subexpressions'.

-- 
Sam Holden



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

Date: Mon, 06 Jan 2003 23:08:23 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Q: Easy regular expression available for this...
Message-Id: <3E1A52B7.B37A6A2A@earthlink.net>

smallya wrote:
> 
> I have text which is of the form:
> {sometext}{more text {even more text}} {{yet more text}{blah blah}}
> which I want to break into:
> {sometext}
> {more text {even more text}}
> {{yet more text}{blah blah}}
> 
> I want to break the line into parts enclosed by only the outermost
> braces. Is there an easy way to do it with regular expressions?

Err, sortof.  First, define a regex which matches nested groups.  This
one here is not quite the same as the example in perlre; I haven't
benchmarked it, but I think it should be slightly faster:

   my $re; $re = qr/
      \{ (?> (?:
         (?: [^{}]+ )    # Non-{}s
      |
         (?= \{ )        # simple lookahead as an optomization.
         (??{ $re })     # Group with matching {}s
      )* ) \}
   /x;

Whether or not you call that easy depends on how well you grock (??{}).

(The (?>) thing, and the (?=\{) thing are just optomizations for this
particular situation, so if you don't understand them, just ignore
them.)

Then, with that, capture all groups at the topmost level of {}s:

   my @groups = $text =~ /($re)/g;

This [untested] code should result in a 3 element array:
   '{sometext}', '{more text {even more text}}',
   '{{yet more text}{blah blah}}'

Note that I *don't* put the capturing parens inside of $re ... that
would (I think) result in this:
   '{sometext}', '{more text {even more text}}', '{even more text}',
   '{{yet more text}{blah blah}}', '{yet more text}', '{blah blah}'
Which I doubt is what you want.


-- 
$..='(?:(?{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: 7 Jan 2003 03:44:43 GMT
From: alnesbit@students.cs.mu.OZ.AU (Andrew Luke NESBIT)
Subject: Recreating directory hierarchy
Message-Id: <avdifb$gai$1@mulga.cs.mu.OZ.AU>

Hi, I have a wide and deep directory hierarchy containing files that
need to be passed through a filter. I want to write some Perl to
traverse this hierarchy, process the files, and write the output files
in a duplicate hierarchy in another part of the disk.

Is this a standard enough thing to do such that there'd be an
already-existing module? Or perhaps some sample code on how to do this?
(I'm quite new to Perl, so I'm not too sure where to begin for this
task.)

Thanks in advance.

Andrew.



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

Date: Mon, 06 Jan 2003 23:23:28 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Recreating directory hierarchy
Message-Id: <3E1A5640.BF1846C8@earthlink.net>

Andrew Luke NESBIT wrote:
> 
> Hi, I have a wide and deep directory hierarchy containing files that
> need to be passed through a filter. I want to write some Perl to
> traverse this hierarchy, process the files, and write the output files
> in a duplicate hierarchy in another part of the disk.
> 
> Is this a standard enough thing to do such that there'd be an
> already-existing module?

Well, the heirarchy traversing thing is fairly standard; you would use
File::Find, which comes with perl.

For example, consider the following:

   use File::Find;
   @ARGV == 2 or die "Usage: $0 source-dir dest-dir\n";
   chdir( $ARGV[0] ) or die "chdir $ARGV[0] failed: $!";
   (my $dest_dir = $ARGV[1]) =~ s,/\z?,/,;
   find( sub {
      return unless -d or -f _;
      (my $dest = $File::Find::name) =~ s,^\./,$dest_dir,
         or die "Malformed filename $File::Find::Name,".
                " expected leading ./\n";
      print "Writing to $dest [press enter]\n"; <>; # debugging.
      return if -e $dest;
      if( -d ) {
         mkdir($dest) or die "mkdir $dest failed: $!"
         return;
      }
      open( my($i), "<", $_ ) or die "Couldn't open $_: $!";
      open( my($o), ">", $dest ) or die "Couldn't open $dest: $!";
      while( my $line = <$i> ) {
         # transform $line
         print $o $line or last;
      }
      close $o or die "Error closing $dest: $!";
   }, "./" );
   __END__
[untested]

> Or perhaps some sample code on how to do this? (I'm quite new to Perl,
> so I'm not too sure where to begin for this task.)
> 
> Thanks in advance.
> 
> Andrew.

-- 
$..='(?:(?{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: Tue, 07 Jan 2003 04:54:00 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Recreating directory hierarchy
Message-Id: <I3tS9.8387$xb.2525@nwrddc02.gnilink.net>

Andrew Luke NESBIT wrote:
> Hi, I have a wide and deep directory hierarchy containing files that
> need to be passed through a filter. I want to write some Perl to
> traverse this hierarchy,

No need to reinvent the wheel. File::Find does that part already.

> process the files,

This part you need to implement

> and write the output files
> in a duplicate hierarchy in another part of the disk.

And this part is standard 'open' and 'print'.

jue




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

Date: Tue, 07 Jan 2003 04:39:36 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: Running CGI scripts offline
Message-Id: <cSsS9.1244$db.157@nwrddc04.gnilink.net>

Cmps wrote:
> My question involves running CGI scripts while offline.  I don't use
> UNIX and only an Internet Explorer browser.  The problem is when I am
> not connected and try to run a CGI script by loading it in the
> browser, it prompts Open... or Save...

Well, what do you expect? A browser will interpret and render HTML files. It
doesn't know anything about Perl scripts, therefore it will do the best it
can: assume you meant to save this file.

> If online and the script is
> in a server's non CGI-BIN directory it does the same.

Maybe. Depends on the configuration of the web server.

> It will only
> work uploading the file to CGI-BIN and calling it from there.  No
> other directory.

Again, depends on how the web server is configured.

> Besides using the ActivePerl from the MS-DOS prompt
> to just test it with no graphical interface, is there some kind of
> options to set in the Internet Explorer to run scripts correctly?

How? Browsers know about HTML. They don't know anything about Perl (or C or
Lisp or Modula or Cobol or ...). Or would you try to feed a C compiler come
Fortran code, too?

> Is there a quick and easy Win32 MicroSoft Visual Studio like compiler
> to run Perl scripts?

No need for. The command line is Perl's home. That's where you run Perl
script. There is nothing quicker or easier than that. No GUI that gets in
your way.

You seem to confuse Perl and CGI. These two have nothing to do with each
other except that Perl is often used to implement CGI programs. But that is
just one of many possible choices for a programming language for CGI
programs and only one of many application areas for Perl programs. This NG
is about Perl. It is not the right place for CGI.

I guess your real question is: how can I run a CGI(!) program (in whatever
language) without uploading it to my web server. Answer: install your own
web server.

Of course this has nothing to do with Perl at all.

jue




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

Date: Tue, 07 Jan 2003 02:03:47 GMT
From: "Matt Morton-Allen" <me@quietplaceiwithnospam.com>
Subject: Shortcut for lvalue issue?
Message-Id: <7AqS9.1202$9f.27692@news.optus.net.au>

Hi All,
I have the following code in one of my programs and I'm looking for a way to
do it without using a temporary variable. I can't seem to find a way without
running into the "Can't modify non-lvalue subroutine call" problem. Any
ideas?

 $temp = $self->address;
 $temp =~ s/\n/<br \/>/g;
 $self->address($temp);

Thanks,
Matt.




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

Date: Mon, 06 Jan 2003 22:50:47 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Shortcut for lvalue issue?
Message-Id: <3E1A4E97.BC3CFD4B@earthlink.net>

Matt Morton-Allen wrote:
> 
> Hi All,
> I have the following code in one of my programs and I'm looking for a
> way to do it without using a temporary variable. I can't seem to find
> a way without running into the "Can't modify non-lvalue subroutine
> call" problem. Any ideas?
> 
>  $temp = $self->address;
>  $temp =~ s/\n/<br \/>/g;
>  $self->address($temp);

Yes, you have to add the ":lvalue" attribute to sub address.
See perldoc perlsub.

-- 
$..='(?:(?{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: Tue, 07 Jan 2003 03:49:14 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Shortcut for lvalue issue?
Message-Id: <3E1A4DAC.5440220C@acm.org>

Matt Morton-Allen wrote:
> 
> I have the following code in one of my programs and I'm looking for a way to
> do it without using a temporary variable. I can't seem to find a way without
> running into the "Can't modify non-lvalue subroutine call" problem. Any
> ideas?
> 
>  $temp = $self->address;
>  $temp =~ s/\n/<br \/>/g;
>  $self->address($temp);

Look up the "Lvalue subroutines" section of the perlsub document.


John
-- 
use Perl;
program
fulfillment


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

Date: Tue, 07 Jan 2003 04:28:16 GMT
From: "M2" <me@quietplaceiwithnospam.com>
Subject: Re: Shortcut for lvalue issue?
Message-Id: <AHsS9.1209$9f.27915@news.optus.net.au>

Thanks.

Upon reading this I'm thinking this could be flying a little close to the
wind. Back to the temp vars I go.


"Benjamin Goldberg" <goldbb2@earthlink.net> wrote in message
news:3E1A4E97.BC3CFD4B@earthlink.net...
> Matt Morton-Allen wrote:
> >
> > Hi All,
> > I have the following code in one of my programs and I'm looking for a
> > way to do it without using a temporary variable. I can't seem to find
> > a way without running into the "Can't modify non-lvalue subroutine
> > call" problem. Any ideas?
> >
> >  $temp = $self->address;
> >  $temp =~ s/\n/<br \/>/g;
> >  $self->address($temp);
>
> Yes, you have to add the ":lvalue" attribute to sub address.
> See perldoc perlsub.
>
> --
> $..='(?:(?{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: Tue, 07 Jan 2003 04:43:59 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: top-posting (was Re: regexp question)
Message-Id: <jWsS9.8349$xb.750@nwrddc02.gnilink.net>

Kasp wrote:
> Also, I learned that my new reading service provider needs me to
> write more new text than the amount of old text already present.

Really? Wow! I wouldn't have thought that there are still News-Provider out
there which enforce a decent posting style. Congratulation that you were
able to find one!

jue




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

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


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