C++ in perl.

2001-06-08 Thread kevin

Hey all,

I'm rather new to perl, and was just woundering what was the best route to
go to Exec c++ from within a perl script. Is there a PM for this?



thank in advance.







Re: C++ in perl.

2001-06-08 Thread Chas Owens

On 08 Jun 2001 09:45:09 -0400, [EMAIL PROTECTED] wrote:
> Hey all,
> 
> I'm rather new to perl, and was just woundering what was the best route to
> go to Exec c++ from within a perl script. Is there a PM for this?
> 
> 
> 
> thank in advance.
> 
> 
> 

Having never done it myself, I can only point you to perldoc perlxs.

--
Today is Prickle-Prickle, the 13rd day of Confusion in the YOLD 3167
Grudnuk demand sustenance!





Re: C++ in perl.

2001-06-08 Thread Peter_Farrar

how about system("some command line"); ?


|+--->
||  [EMAIL PROTECTED]|
||  m|
||   |
||  06/08/01 |
||  09:45 AM |
||   |
|+--->
  >|
  ||
  |   To: [EMAIL PROTECTED]   |
  |   cc: (bcc: Peter Farrar/Waltham/MCS/Concentra)|
  |       Subject: C++ in perl.|
  >|



Hey all,

I'm rather new to perl, and was just woundering what was the best route to
go to Exec c++ from within a perl script. Is there a PM for this?



thank in advance.











Re: C++ in perl.

2001-06-08 Thread Jeff Yoak

At 09:45 AM 6/8/01 -0400, [EMAIL PROTECTED] wrote:
>Hey all,
>
>I'm rather new to perl, and was just woundering what was the best route to
>go to Exec c++ from within a perl script. Is there a PM for this?

I haven't used it personally, but there has been a fair amount of talk 
about Inline.pm .  You might look into it.

Cheers,
Jeff





Embedding C in Perl

2002-08-01 Thread Andrew Killam

Hi, guys.  This is my first message to the list, so if I'm violating any posting 
procedures, please forgive me - I'll learn quickly enough.

I'm working a program manipulating trees read in from a database.  My employers want 
Perl used, but I believe parts of it would be better served using C functions and 
structs.

Does anyone know how to write C code in a Perl script?  Could you please point me to 
tutorials or similar sites, if you know of them?

thanks,
Andrew

[EMAIL PROTECTED]



GNU grep -C in Perl

2002-12-10 Thread Jerry Rocteur
Hi,

Anyone know how to write a grep -C in Perl ?

You know, I want to search for a string/regexp in a file and I want to 
print the line before and the line after the search string.

I've tried a couple of ways and it takes a really long time, I load the 
file in memory, seach for the string, extract the line number of the 
string $. subtract one and add one and print the three lines but this 
can take a very long time.. I don't have grep -C on the system I want 
to do this.

I've looked at perldoc and the list archives and can't fing any help..

Thanks in advance,

Jerry


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: Embedding C in Perl

2002-08-01 Thread Paul Johnson

On Thu, Aug 01, 2002 at 10:13:42AM -0700, Andrew Killam wrote:

> Hi, guys.  This is my first message to the list, so if I'm violating
> any posting procedures, please forgive me - I'll learn quickly enough.

The only thing I would complain about is that your lines are too long,
but I'd aim that more at hotmail than you, I suppose.

> I'm working a program manipulating trees read in from a database.  My
> employers want Perl used, but I believe parts of it would be better
> served using C functions and structs.

It would be interesting to know which parts and why.  Is this for speed
purposes, or some other reason.  You'll almost always spend _your_ time
better working with a higher level language, where you have a reasonable
choice.

> Does anyone know how to write C code in a Perl script?  Could you
> please point me to tutorials or similar sites, if you know of them?

I would checkout Inline.pm.  Look on search.cpan.org.

Other options include XS and swig, but I would start with Inline::C.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: GNU grep -C in Perl

2002-12-10 Thread Larry Coffin
>Anyone know how to write a grep -C in Perl ?

Why not just store the previous line, then when you have a match,
print it, the matching line, then set a flag to print the next line as well?

Something along the lines of:

while ($line = ) {
if ($print_next) {
# previous line matched, print this line as following "context"
print $line;
$print_next = 0;
}

if ($line =~ /$pattern/) {
print $last;
print $line;
$print_next = 1; # flag to print the next line
}

$last = $line;
}

That should work unless you want to make sure you never print the
same line twice or if you want more than one line of context.

---Larry


++
| Larry Coffin, G.P.H. Watertown, MA |
| http://www.PointInfinity.com/lcoffin/[EMAIL PROTECTED] |
++

Money is the root of all evil, and man needs roots


-



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: GNU grep -C in Perl

2002-12-10 Thread Jerry Rocteur
Hi,

Reading further in the archives to this list I got an idea: (thanks to 
John W. Krahn)

#!/usr/bin/perl
$pattern = shift(@ARGV);
while (<>) {
if ( /$pattern/i ){
print $reml;
print $_;
$_ = <>;
print "$_\n";
}
$reml = $_;
}

That is certainly much quicker than the way I was doing it before..

I'd still like to see if there is a cleaner way though.

Also, to make it more flexible, perhaps print more than one line before 
and more than one line after.

TIA,

Jerry

On Tuesday, Dec 10, 2002, at 21:22 Europe/Brussels, Jerry Rocteur wrote:

Hi,

Anyone know how to write a grep -C in Perl ?

You know, I want to search for a string/regexp in a file and I want to 
print the line before and the line after the search string.

I've tried a couple of ways and it takes a really long time, I load 
the file in memory, seach for the string, extract the line number of 
the string $. subtract one and add one and print the three lines but 
this can take a very long time.. I don't have grep -C on the system I 
want to do this.

I've looked at perldoc and the list archives and can't fing any help..

Thanks in advance,

Jerry


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: GNU grep -C in Perl

2002-12-10 Thread Hanson, Rob
Another option would be to use the Perl version of grep from the PPT (Perl
Power Tools) project.

PPT Home
http://www.perl.com/language/ppt/

tcgrep Source
http://www.perl.com/language/ppt/src/grep/tcgrep

tcgrep Perldoc
http://www.perl.com/language/ppt/src/grep/tcgrep.html


Rob

-Original Message-
From: Jerry Rocteur [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, December 10, 2002 3:23 PM
To: [EMAIL PROTECTED]
Subject: GNU grep -C in Perl 


Hi,

Anyone know how to write a grep -C in Perl ?

You know, I want to search for a string/regexp in a file and I want to 
print the line before and the line after the search string.

I've tried a couple of ways and it takes a really long time, I load the 
file in memory, seach for the string, extract the line number of the 
string $. subtract one and add one and print the three lines but this 
can take a very long time.. I don't have grep -C on the system I want 
to do this.

I've looked at perldoc and the list archives and can't fing any help..

Thanks in advance,

Jerry


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: GNU grep -C in Perl

2002-12-10 Thread Jason Tiller
Hi, Jerry, :)

On Tue, 10 Dec 2002, Jerry Rocteur wrote:

> Anyone know how to write a grep -C in Perl ?

Here is a script that mimics just the '-C' feature of GNU grep.  It
doesn't support the fanciness of printing filenames when multiple
input files are specified.

This may be overkill for your situation, but you can specify an
arbitrary number of context lines.  Also, this script (unlike all
others mentioned, I believe) will never print duplicate lines in case
the context of two matches overlaps.

This was a fun problem.  I'm sure it can be done in more efficient
ways.  I'd love to see another's solution to this.

If you have any questions, don't hesitate to ask. :)

Enjoy!

---Jason



#!/usr/bin/perl

# We're trying to mimic 'grep -C', which prints a certain number of
# context lines around each match.  So, if a match occurred on line 10
# in a file invoked with 'grep -C 5', then the user would see lines 5
# thru 15 on their terminal (5 lines before, the match line, and 5
# lines after).
#
# However, grep -C *never* prints the same line twice, so if two
# matches occur within the "context", then grep -C is smart enough to
# not reprint any of the previous match's context.  For example, if
# there was a match on line 10 & on line 12, then grep -C would print
# lines 5 thru 17.  Cute, huh?
#
# Invoke with:
#
# grep-c.pl [--debug] [--context NUM] regex file1 file2...
#

use strict;
use warnings;
use Getopt::Long;

# Set to true if you want debugging output.
my $debug = 0; # Default.

# Number of context lines to print.
my $num_ctxt = 2; # Default.

GetOptions( 'debug' => \$debug, 'context=i' => \$num_ctxt )
   or die "$!\n";

# User passes the regex on the command line.
my $re = shift @ARGV or die "Need regex!\n";

# Unprinted lines that we have already seen.
my @back_context;

# "Future" lines that we have yet to check against the regex.  These
# lines will already have been printed.
my @next_lines;

# Current line.
my $line;

# If there are future lines that we've printed but haven't yet tested
# against our regex, then read from that list.  Else go to the file
# itself and request the next line.
while( @next_lines or defined($_ = <>) ) {
   print "At loop: \$_ = $_" if $debug;

   # The logic is this: @back_context only stores previous lines that
   # we have not yet printed.  Since all of the lines in @next_lines
   # are printed when we have a match, we don't update @back_context
   # when reading lines from @next_lines.
   if( @next_lines ) {
  $line = shift @next_lines;
   } else {
  $line = $_;
  push @back_context, $line;
  shift @back_context if $#back_context > $num_ctxt;
   }

   print "\$line = $line, #next_lines = " . @next_lines .
 ", #back_context = " . @back_context . "\n"
if $debug;

   next unless $line =~ /$re/;

   # Unprinted lines that we have yet to see.  We want to never print
   # duplicate lines, so @forward_context only holds lines that are
   # later in the file than @next_lines (which have already been
   # printed).
   my @forward_context;

   # Lookahead so that we have at least $num_ctxt lines ahead in the
   # file.  (If the user didn't want any context, $num_ctxt will be 0
   # and we'll have nothing to print.)
   while( $num_ctxt && defined($_ = <>) ) {
  print "Adding to \@next: $_" if $debug;
  push @next_lines, $_;
  push @forward_context, $_;
  last if @next_lines >= $num_ctxt;
   }

   print foreach @back_context, @forward_context;

   # There are no more unprinted previously seen lines, since we just
   # printed them all!
   undef @back_context;

   # Are there any lines left in the file?
   exit unless defined $_;
}


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: GNU grep -C in Perl

2002-12-10 Thread John W. Krahn
Jerry Rocteur wrote:
> 
> Hi,

Hello,

> Anyone know how to write a grep -C in Perl ?

Yes, someone definitely does:
http://www.perl.com/language/ppt/src/grep/index.html


> You know, I want to search for a string/regexp in a file and I want to
> print the line before and the line after the search string.
> 
> I've tried a couple of ways and it takes a really long time, I load the
> file in memory, seach for the string, extract the line number of the
> string $. subtract one and add one and print the three lines but this
> can take a very long time.. I don't have grep -C on the system I want
> to do this.
> 
> I've looked at perldoc and the list archives and can't fing any help..


my $before = 1;
my $after  = 1;
my @buffer;

while ( my $line =  ) {

push @buffer, $line;

shift @buffer if $#buffer > $before + $after;

next if $#buffer < $before;

if ( $buffer[ $before ] =~ /found matching regex/ ) {

print @buffer;

}
}

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: GNU grep -C in Perl

2002-12-10 Thread John W. Krahn
"John W. Krahn" wrote:
> 
> my $before = 1;
> my $after  = 1;
> my @buffer;
> 
> while ( my $line =  ) {
> 
> push @buffer, $line;
> 
> shift @buffer if $#buffer > $before + $after;
> 
> next if $#buffer < $before;
> 
> if ( $buffer[ $before ] =~ /found matching regex/ ) {
> 
> print @buffer;
> 
> }
> }
> 
> __END__


Sorry about that, this should work better:

my $before = 1;
my $after  = 1;
my @buffer;

while ( my $line =  ) {
push @buffer, $line;
shift @buffer if $#buffer > $before + $after;
next if $#buffer < $before;
if ( $buffer[ $before ] =~ /found matching regex/ ) {
print @buffer;
@buffer = ();
}
}

__END__



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




static variables and sub-routines with file-scope (like C) in Perl

2002-08-15 Thread Dharmendra Rai


hi,

You may declare my  variables at the outermost scope of a file to hide any such 
identifiers from the world outside that file. This is similar  to C's static variables 
when they are used at the file level. To do this with a subroutine requires the use of 
a closure (an anonymous function that accesses enclosing lexicals). If you want to 
create a private subroutine that cannot be called from outside that block, it can 
declare a lexical variable containing an anonymous sub reference: 

package hello;

my $static_var = 23;
my $static_func = sub { print $secret_version };
&$secret_sub();  

Only the functions defined in this scope can access the variable as shown above. Also 
as long as the reference is never returned by any function within the module, no 
outside module can see the subroutine, because its name is not in any package's symbol 
table. One can't access the variable defined above by $hello::static_var in any other 
file . Also , this variable is not like a static variable in a function (like C) . It 
is like an "auto" variable in C.




-
Get a bigger mailbox -- choose a size that fits your needs.

http://uk.docs.yahoo.com/mail_storage.html