david.jackson has asked for the wisdom of the Perl Monks concerning the following question:

How can I change the end_of_field char. to create multi-line input fields for command line scripts? Thanks David

Replies are listed 'Best First'.
Re: Multi-line input fields
by pixel (Scribe) on Oct 25, 2001 at 13:02 UTC

    There ar some special values that you can set $/ that give you various useful behaviours.

    • $/ = undef puts Perl in "slurp" mode. The next read from a filehandle will read in all of the remaining records in the file.
    • $/ = '' puts Perl in "paragraph" mode. In this mode, records are delimited by one or more blank lines.
    • Setting $/ to a reference to a number (or a reference to a scalar holding a number) sets the record size to that number. The next read from a filehandle will read in that number of bytes.

    Like all Perl special variables, you'll usually want to alter its value localised in a block.

    my $text = do { local $/; $text = <FILE> };

    Blessed Be
    The Pixel

      Thanks for your replies. I believe this was what I was looking for. David
Re: Multi-line input fields
by davis (Vicar) on Oct 25, 2001 at 12:20 UTC
    The variable $/ contains the record separator ("perldoc perlvar"), which is what I believe you're after.
    you can explicitly set it to match a couple of newlines, so that it'll separate on a blank line. Eg:
    #!/usr/bin/perl -w use strict; my $input; $/ = "\n\n"; $input = <STDIN>; print "You said:\n$input\n";
    I think that'll do what you're after.
    Update: pixel's answer's better. I'd forgotten about paragraph mode
Re: Multi-line input fields
by davorg (Chancellor) on Oct 25, 2001 at 12:18 UTC

    Do you mean "end of field" or "end of record"? If it's end of record then you should look at the $/ variable.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Followup: Multi-line input fields
by david.jackson (Novice) on Oct 25, 2001 at 15:26 UTC
    OK -- We're getting close. See what you think? One of the problems still reamaining is escaping "@" in email address. If anyone insterested this is a little c.y.a script I'm creating to track server outages . Thanks again David
    #!/usr/bin/perl -w use strict; use DBI; my ($dbh,$sth,@row,$server,$date,$date_closed,$contact,$contact_email, +$contact_phone,$planned,$event,$resolution); print "Enter sever name: "; $server=<STDIN>; chomp($server); print "Enter Date (YYYY/MM/DD): ";$date=<STDIN>;chomp($date); print "Date Closed(YYYY/MM/DD): ";$date_closed=<STDIN>;chomp($date_clo +sed); print "Contact: ";$contact=<STDIN>;chomp($contact); #print "Contact Email: ";$contact_email=<STDIN>;chomp($contact_email); print "Contact Phone: ";$contact_phone=<STDIN>;chomp($contact_phone); print "Planned Event(yes/no): ";$planned=<STDIN>;chomp($planned); ###################################################################### +###### # Define multi-line fields $/ = "^A"; print "Describe Event:\n"; $event=<STDIN>;chomp($event); print "\n"; print "Describe Resolution:\n"; $resolution=<STDIN>;chomp($resolution); ###################################################################### +######## $dbh = DBI->connect("dbi:mysql:sirinfo","rca","rcaS7ay0u7") or die "Nice try buckaroo\n"; $sth = $dbh->prepare("INSERT INTO rca(server,date,date_closed, contact,contact_phone,planned,event,resolution VALUES('$server','$date','$date_closed','$contact +','$contact_phone','$planned','$event','$resoluti on')") or die "No cigar\n"; $sth->execute(); $dbh->disconnect(); exit;

      You should *really* be using placeholders with DBI. Really. See Tricks with DBI for links to why. At the least you need to escape your strings. You can also do this which is a shorter idiom when getting input:

      chomp(my input = <STDIN>); # get input and remove newline

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print