CINXE.COM

if.pm - metacpan.org

<!DOCTYPE html> <html lang="en-US"> <head> <title>if.pm - metacpan.org</title> <link rel="preload" as="fetch" href="/account/login_status" crossorigin="anonymous" /> <link href="/assets/style-XFEQ536G.css" rel="stylesheet" type="text/css"> <script src="/assets/main-WXBSEDQJ.js" type="module"></script> <link rel="alternate" type="application/rss+xml" title="Recent CPAN Uploads of if - MetaCPAN" href="/dist/if/releases.rss" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5"> <link rel="shortcut icon" href="/static/icons/favicon.ico"> <link rel="apple-touch-icon" sizes="152x152" href="/static/icons/apple-touch-icon.png"> <link rel="search" href="/static/opensearch.xml" type="application/opensearchdescription+xml" title="MetaCPAN"> <script async src="https://perl-ads.perlhacks.com/perl-ads.js"></script> <script async src="https://www.googletagmanager.com/gtag/js?id=G-E82Q2V8LVD"></script> </head> <body> <nav class="navbar navbar-default" role="navigation"> <div class="header-logo-large hidden-xs"> <a href="/" tabindex="0"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#logo" /> </svg> </a> </div> <div class="header-logo-icon visible-xs"> <a href="/"> <svg class="logo" aria-label="MetaCPAN"> <use class="logo" href="/static/images/metacpan-logo.svg#dots" /> </svg> </a> </div> <ul class="nav navbar-nav menu-items hidden-xs hidden-sm"> <li><a href="/about">About</a></li> <li><a href="/about/sponsors">Sponsor</a></li> <li><a href="https://grep.metacpan.org/">grep::cpan</a></li> <li><a href="/recent">Recent</a></li> <li><a href="/about/faq">FAQ</a></li> <li><a href="/tools">Tools</a></li> <li><a href="https://fastapi.metacpan.org/">API</a></li> </ul> <ul class="nav navbar-nav navbar-right"> <button type="button" class="searchbar-btn visible-xs visible-sm"> <i class="fa fa-search button-fa-icon"></i> </button> <form action="/search" class="searchbar-form visible-md visible-lg search-form form-horizontal"> <input type="hidden" name="size" id="metacpan_search-size" value="20"> <div class="form-group"> <div class="search-group"> <i class="fa fa-search"></i> <input type="text" name="q" placeholder="Search the CPAN" size="41" autocorrect="off" autocapitalize="off" spellcheck="false" id="metacpan_search-input" class="form-control" value=""> </div> </div> </form> <li class="icon-slidepanel visible-xs visible-sm"> <button data-toggle="slidepanel" data-target=".slidepanel"> <span class="button-fa-icon"> <i class="fa fa-bars slidepanel-open"></i> <i class="fa fa-times slidepanel-close"></i> </span> </button> </li> <li class="dropdown login-dropdown show-logged-in"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon logged-in-icon" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li><a href="/account/identities">Identities</a></li> <li><a href="/account/profile">Profile</a></li> <li><a href="/account/favorite/list">Favorites</a></li> <li> <a href="#" type="button" class="logout-button"> Logout </a> </li> </ul> </li> <li class="dropdown login-dropdown show-logged-out"> <button type="button" class="dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-user button-fa-icon avatar-placeholder" aria-hidden="true"></i> <i class="fas fa-chevron-down"></i> </button> <ul class="dropdown-menu"> <li> <a href="/login/github"> <i class="fab fa-github fa-fw"></i> GitHub </a> </li> <li> <a href="/login/twitter"> <i class="fab fa-twitter fa-fw"></i> Twitter </a> </li> <li> <a href="/login/google"> <i class="fab fa-google fa-fw"></i> Google </a> </li> </ul> </li> <li class="dropdown login-dropdown hide-logged-in hide-logged-out"> <button> <i class="fa fa-user button-fa-icon" aria-hidden="true"></i> </button> </li> </ul> </nav> <div class="page-content "> <div id="perl-ad-target" class="top-notify-banner perl-ad-target"> </div> <nav class="sidebar"> <div class="slidepanel"> <ul class="nav-list "> <li class="nav-header">Tools</li> <li> <a data-keyboard-shortcut="g d" href="/release/XSAWYERX/if-0.0608">Release Info</a> </li> <li> <a href="/pod/if">Module Documentation</a> </li> <li> <a data-keyboard-shortcut="g a" href="/author/XSAWYERX">Author</a> </li> <li>&nbsp;</li> <li><a href="https://fastapi.metacpan.org/source/XSAWYERX/if-0.0608/if.pm">Raw code</a></li> <li><a href="/release/XSAWYERX/if-0.0608/source/if.pm">Permalink</a></li> <li> <a href="/release/XSAWYERX/if-0.0608/raw/if.pm?download=1">Download</a> </li> <li><button class="btn-link pod-toggle pod-hidden"><span class="hide-pod">Hide</span><span class="show-pod">Show</span> Pod</button></li> <li class="nav-header">Info</li> <li>18 lines of code</li> <li>58 lines of pod</li> <li>3.53K bytes</li> </ul> </div> </nav> <div class="content-navigation"> <div class="breadcrumbs"> <a data-keyboard-shortcut="g s" href="/release/XSAWYERX/if-0.0608/source">XSAWYERX / if-0.0608</a> / if.pm </div> </div> <main class="content"> <pre id="metacpan_source" class="line-numbers pod-toggle pod-hidden" data-pod-lines="25-116"><code class="language-perl">package if; $VERSION = &#39;0.0608&#39;; sub work { my $method = shift() ? &#39;import&#39; : &#39;unimport&#39;; unless (@_ &gt;= 2) { my $type = ($method eq &#39;import&#39;) ? &#39;use&#39; : &#39;no&#39;; die &quot;Too few arguments to &#39;$type if&#39; (some code returning an empty list in list context?)&quot; } return unless shift; # CONDITION my $p = $_[0]; # PACKAGE (my $file = &quot;$p.pm&quot;) =~ s!::!/!g; require $file; # Works even if $_[0] is a keyword (like open) my $m = $p-&gt;can($method); goto &amp;$m if $m; } sub import { shift; unshift @_, 1; goto &amp;work } sub unimport { shift; unshift @_, 0; goto &amp;work } 1; __END__ =head1 NAME if - C&lt;use&gt; a Perl module if a condition holds =head1 SYNOPSIS use if CONDITION, &quot;MODULE&quot;, ARGUMENTS; no if CONDITION, &quot;MODULE&quot;, ARGUMENTS; =head1 DESCRIPTION =head2 C&lt;use if&gt; The C&lt;if&gt; module is used to conditionally load another module. The construct: use if CONDITION, &quot;MODULE&quot;, ARGUMENTS; ... will load C&lt;MODULE&gt; only if C&lt;CONDITION&gt; evaluates to true; it has no effect if C&lt;CONDITION&gt; evaluates to false. (The module name, assuming it contains at least one C&lt;::&gt;, must be quoted when C&lt;&#39;use strict &quot;subs&quot;;&#39;&gt; is in effect.) If the CONDITION does evaluate to true, then the above line has the same effect as: use MODULE ARGUMENTS; For example, the F&lt;Unicode::UCD&gt; module&#39;s F&lt;charinfo&gt; function will use two functions from F&lt;Unicode::Normalize&gt; only if a certain condition is met: use if defined &amp;DynaLoader::boot_DynaLoader, &quot;Unicode::Normalize&quot; =&gt; qw(getCombinClass NFD); Suppose you wanted C&lt;ARGUMENTS&gt; to be an empty list, I&lt;i.e.&gt;, to have the effect of: use MODULE (); You can&#39;t do this with the C&lt;if&gt; pragma; however, you can achieve exactly this effect, at compile time, with: BEGIN { require MODULE if CONDITION } =head2 C&lt;no if&gt; The C&lt;no if&gt; construct is mainly used to deactivate categories of warnings when those categories would produce superfluous output under specified versions of F&lt;perl&gt;. For example, the C&lt;redundant&gt; category of warnings was introduced in Perl-5.22. This warning flags certain instances of superfluous arguments to C&lt;printf&gt; and C&lt;sprintf&gt;. But if your code was running warnings-free on earlier versions of F&lt;perl&gt; and you don&#39;t care about C&lt;redundant&gt; warnings in more recent versions, you can call: use warnings; no if $] &gt;= 5.022, q|warnings|, qw(redundant); my $test = { fmt =&gt; &quot;%s&quot;, args =&gt; [ qw( x y ) ] }; my $result = sprintf $test-&gt;{fmt}, @{$test-&gt;{args}}; The C&lt;no if&gt; construct assumes that a module or pragma has correctly implemented an C&lt;unimport()&gt; method -- but most modules and pragmata have not. That explains why the C&lt;no if&gt; construct is of limited applicability. =head1 BUGS The current implementation does not allow specification of the required version of the module. =head1 SEE ALSO L&lt;Module::Requires&gt; can be used to conditionally load one or modules, with constraints based on the version of the module. Unlike C&lt;if&gt; though, L&lt;Module::Requires&gt; is not a core module. L&lt;Module::Load::Conditional&gt; provides a number of functions you can use to query what modules are available, and then load one or more of them at runtime. The L&lt;provide&gt; module from CPAN can be used to select one of several possible modules to load based on the version of Perl that is running. =head1 AUTHOR Ilya Zakharevich L&lt;mailto:ilyaz@cpan.org&gt;. =head1 COPYRIGHT AND LICENCE This software is copyright (c) 2002 by Ilya Zakharevich. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. =cut </code></pre> </main> <div class="content-pagination"> </div> </div> <footer class="footer"> <div class="footer-container"> <div class="footer-social"> <div class="footer-link footer-logo"> <a href="/"> <img src="/static/images/metacpan-logo.svg" alt="MetaCPAN" /> </a> </div> <a class="footer-social-link" href="https://github.com/metacpan"> <i class="fab fa-github-square"></i> </a> <a class="footer-social-link" href="https://fosstodon.org/@metacpan"> <i class="fab fa-mastodon"></i> </a> </div> <div class="footer-links"> <div class="footer-link"> <a href="/about">About</a> </div> <div class="footer-link"> <a href="/about/sponsors">Sponsor</a> </div> <div class="footer-link"> <a href="https://grep.metacpan.org">grep::cpan</a> </div> <div class="footer-link"> <a href="/recent">Recent</a> </div> <div class="footer-link"> <a href="/about/faq">FAQ</a> </div> <div class="footer-link"> <a href="/tools">Tools</a> </div> <div class="footer-link"> <a href="https://fastapi.metacpan.org/">API</a> </div> <div class="footer-link"> <a href="https://www.perl.org/">Perl.org</a> </div> </div> <div class="footer-sponsors"> <a class="footer-sponsor-link" target="_blank" href="https://www.bytemark.co.uk/" rel="noopener"> <img class="footer-sponsor-bytemark" src="/static/images/sponsors/bytemark_logo.svg" alt="Bytemark logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.liquidweb.com/" rel="noopener"> <img class="footer-sponsor-liquidweb" src="/static/images/sponsors/liquidweb_logo.png" alt="liquidweb logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://deriv.com/careers/" rel="noopener"> <img class="footer-sponsor-deriv" src="/static/images/sponsors/deriv.svg" alt="Deriv logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://geocode.xyz" rel="noopener"> <img class="footer-sponsor-geocode" src="/static/images/sponsors/geocodelogo.svg" alt="Geocode logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://www.fastly.com/" rel="noopener"> <img class="footer-sponsor-fastly" src="/static/images/sponsors/fastly_logo.svg" alt="Fastly logo"> </a> <a class="footer-sponsor-link" target="_blank" href="https://opencagedata.com" rel="noopener"> <img class="footer-sponsor-opencage" src="/static/images/sponsors/open-cage.svg" alt="OpenCage logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://www.elastic.co/" rel="noopener"> <img class="footer-sponsor-elastic" src="/static/images/sponsors/elastic.svg" alt="Elastic logo"> </a> <!-- Added 2024-07-22 --> <a class="footer-sponsor-link" target="_blank" href="https://route4me.com/" rel="noopener"> <img class="footer-sponsor-route4me" src="/static/images/sponsors/route4me.png" alt="Route4Me logo"> </a> </div> </div> </footer> <div class="modal fade" tabindex="-1" role="dialog" id="metacpan_keyboard-shortcuts"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Keyboard Shortcuts</h4> </div> <div class="modal-body row"> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Global</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>s</kbd> </td> <td>Focus search bar</td> </tr> <tr> <td class="keys"> <kbd>?</kbd> </td> <td>Bring up this help dialog</td> </tr> </tbody> </table> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>GitHub</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>p</kbd> </td> <td>Go to pull requests</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>go to github issues (only if github is preferred repository)</td> </tr> </tbody> </table> </div> <div class="col-md-6"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>POD</th> </tr> </thead> <tbody> <tr> <td class="keys"> <kbd>g</kbd> <kbd>a</kbd> </td> <td>Go to author</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>c</kbd> </td> <td>Go to changes</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>i</kbd> </td> <td>Go to issues</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>d</kbd> </td> <td>Go to dist</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>r</kbd> </td> <td>Go to repository/SCM</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>s</kbd> </td> <td>Go to source</td> </tr> <tr> <td class="keys"> <kbd>g</kbd> <kbd>b</kbd> </td> <td>Go to file browse</td> </tr> </tbody> </table> </div> <div class="col-md-12"> <table class="table keyboard-shortcuts"> <thead> <tr> <th></th> <th>Search terms</th> </tr> </thead> <tbody> <tr> <td><em>module:</em> (e.g. <a href="/search?q=module%3APlugin">module:Plugin</a>)</td> </tr> <tr> <td><em>distribution:</em> (e.g. <a href="/search?q=distribution%3ADancer+auth">distribution:Dancer auth</a>)</td> </tr> <tr> <td><em>author:</em> (e.g. <a href="/search?q=author%3ASONGMU+Redis">author:SONGMU Redis</a>)</td> </tr> <tr> <td><em>version:</em> (e.g. <a href="/search?q=version%3A1.00">version:1.00</a>)</td> </tr> </tbody> </table> </div> </div> <div class="modal-footer"></div> </div> </div> </div> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10