Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

C and other stuff

by perlaintdead (Scribe)
on Jan 12, 2014 at 14:41 UTC ( #1070337=CUFP: print w/replies, xml ) Need Help??

I posted this but then reconsidered because honestly it's really not that great but ww had beef so here's the post:

I made this little module to carry over some things i liked about C as well as some other stuff. Just drop it into your folder and do a  require ''; link: The perldoc doesn't cover all the functions. I need to update it.

UPDATE: implemented most of tobyink's suggestions and corrected spelling of promt to prompt by suggestion of no_slogan
# Copyright 2013,2014 Dakota Simonds # This program is free software: you can redistribute it and/or mod +ify # it under the terms of the GNU General Public License as published + by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see < +s/>. use warnings; use strict; sub EXIT_SUCCESS (){ #C style 1; } sub TRUE (){ 1; } sub FALSE (){ 0; } sub forceflush{ our $| = 1; return EXIT_SUCCESS; } sub savefile{ #savefile(filename, data); my $SVFL; open $SVFL, ">>", $_[0]; syswrite $SVFL, $_[1]; close $SVFL; return EXIT_SUCCESS; } sub assert{ #C style my @assertions = @_; foreach my $testAssertion (@assertions){ if( not eval $testAssertion ){ print "Assertion failure: '$testAssertion'\n"; exit 0; } #else{ print "win\n"; } } return EXIT_SUCCESS; } sub prompt{ my $question = shift; my $style = shift; my $styleOut; my $input; my %styles=( plain => "\x20", normal => ': ', yn => ' (y/n) ? ', ); if($style eq ""){ $styleOut = $styles{"plain"}; } else{ $styleOut = $styles{$style}; } { #these currlies makes redo work when input is not valid print $question, $styleOut; $input = <STDIN>; chomp $input; if(not $input =~ m/(y|n)/i and $style eq "yn"){ print "\nThat is not a valid input!\n"; redo; } } return $input; } sub strcat{ my $compiled; for my $str (@_) { $compiled = $compiled . $str; } return $compiled; } sub longCatIsLoooooooooooooooooooooooooooooooooooooooooooooooooooooooo +oooooooooooooooooooooooooooooooooooooooooooooooonnngggggg{ return ''; } 1; __END__ =head1 NAME Spice =head1 SYNOPSIS technically all these are functions but these three return true or false and take no arguments. meant to reduce magic numbers. EXIT_SUCCESS - C style return (true) TRUE - returns 1 FALSE - returns 0 forceflush - same as $|=1 savefile - appends data to a file assert - C style fuction. Like eval but if code reurns a fal +se the program exits otherwise returns 1. takes a list. prompt - get input in one line strcat - a funtion the concatinates it's inputs savefile - writes to a file in one line =head1 DESCRIPTION Simple PERL module to that fixes small anoying things and provides + a bit of C style funtionality =head1 EXAMPLES forceflush; while( TRUE ){ do something } savefile("meows.txt", "data"); if($foobar == FALSE){ do something } prompt("keywords","normal"); prompt("do you want a cookie", "yn"); prompt("do you want a cookie (Y/n)?", "plain"); assert('$a=1'); savefile($filename, $data); return EXIT_SUCCESS; =cut

Replies are listed 'Best First'.
Re: C and other stuff
by tobyink (Abbot) on Jan 12, 2014 at 19:08 UTC

    Your assert function won't be able to see lexical variables.

    my $x = 42; assert '$x > 40'; # assertion fails, # because eval can't see $x

    Better is to write assert so that it takes a code ref...

    sub assert (&) { require Carp; (shift)->() ? 1 : Carp::croak('Assertion failed') } my $x = 42; assert { $x > 40 }; # assertion ok

    Also, I'd recommend giving your pseudo-constants TRUE, FALSE and EXIT_SUCCESS an empty prototype - i.e. () - and not using the return keyword in them. This allows the Perl compiler to treat them as real constants - inlining them into code that calls them. And if will solve your problems that lead them to occasionally needing to be called with a leading &.

    sub TRUE () { 1 } sub FALSE () { 0 } sub EXIT_SUCCESS () { TRUE }
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: module for C-like functionality
by ww (Bishop) on Jan 12, 2014 at 16:22 UTC
    Now that the OP has removed everything, here's what was actually posted:
    " I made this little module to carry over some things i liked about C. Just drop it into your folder and do a  require '';
    link: google code -
    The perldoc doesn't cover all the functions. I need to update it."

    ... and, of course, we can take it on faith that the content of is NOT something like:

    rm -rf...
    shred -fzun=17 # shred won't do this (or "won't do this well" on some OSen, AIX
    # or journaled or compressed FS or XFS, Ext3... etc
    # nor on RAID boxes.... ) :-)

    Please, if you have something to post, POST IT HERE, not somewhere else from which is may disappear or which foreign source may hide malware.

    Come, let us reason together: Spirit of the Monastery
      yeah that's what i posted but you don't have to take it on faith if you can read perl. it's only 173 lines including the docs
      downvote this post if you hate being happy
        strcat can be written join('',@_). Also, there are two "p"s in "prompt."

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1070337]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-01-24 02:01 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (200 votes). Check out past polls.